Long time no write ... I'm going to resume my blog recently and do my best.
Gives a string that returns the first character that appears only once.
such as: input: Aabbccddeff
Back: F
The first thought is to compare each character with all the characters, there is no same output, time complexity is obviously O (N2).
When I saw this Google's face test, I thought of the other day to see whether or not to use a different or comparison binary to solve two objects are the same problem. But different or only even number of the same value to get 0, the odd number of the same worthy of his own, in this problem is not a good idea. A comment under the title provides a good idea. Store this string with the idea of a hash table. And the structure of the hash table is very simple, the key is only the corresponding ASCII code value, the value of the number of characters should appear. The first traversal string converts the ASCII code of each character and its occurrence here in the table, and the second traversal of the hash table the first value of 1 is the first character that appears only once, with the following code:
<span style= "FONT-SIZE:18PX;" > #include <iostream>
char findfirstcharnotrepeated (char *newstring) {
const int hashlength = 256;
int hashtable[hashlength];
Initialize a simple hash table (key: ASCII value of the letter, value: Number of repetitions) for
(int i = 0; i < hashlength; i++) {
hashtable[i] = 0;
}
Assigns the first address of the array to the pointer newchar
char *newchar = newstring;
First traversal string while
(*newchar!= ' ")
{
hashtable[* (newchar++)]++;
}
Newchar = newstring;
Second traversal string while
(*newchar!= ' ")
{
if (hashtable[*newchar] = = 1) {return
*newchar;
}
newchar++;
}
Return ' 0 ';
}
int main () {
std::cout << "Please input a string:";
const int maxstringlength = m;
Char Target[maxstringlength];
Std::cin >> Target;
Std::cout << "repeated Char:";
Std::cout << findfirstcharnotrepeated (target) << Std::endl;
return 0;
} </span>
The road of the game has a long way to go, I will be up and down and searching.