/*FUNCTION*/ //*FIND 1ST NON-REPEATING****************************************
char find_nonrepeat_char(strLetters) {
//make ascii array to store letter count, then count
{
int countArray[25];
for (int i =0; i < 26; i++) countArray[i] = 0;
for (int i = 0; i < length(strLetters); i++) {
countArray[(int)strLetters[i]-65]++;
}
for (i = 0; i < length(strLetters); i++) {
if (countArray[(int)strLetters[i]-65)] == 1) return strLetters[i];
}
}
Thursday, April 10, 2008
C++: Find 1st non-repeating character
Here is a function that I enjoyed solving. You have to write a function that takes a string input and finds the first non-repeating character in a string (eg. “ABCA” -> B). This code runs in about O(n). Enjoy!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment