Problem Description:
In the string, find the first character that appears only 1 times. such as input "Abaccdeff", Output B. Problem Analysis:
Method 1: The most straightforward and outrageous method.
Implementation method: For each character in the string, go through the string, find whether it is only one occurrence. Thus, the time complexity is O (n*n), and N is the number of characters in the string. Such an approach is often not the best approach.
Method 2: Hash method.
Since the above method time complexity is too high, we can use space in exchange for time Hashifa to solve the problem.
How to implement: Define an array of 256 (why 256). Because a character is 1 bytes, 8 bits, can represent 256 characters, statistics in the number of characters in the string, after the statistics are complete, and then find the first occurrence of 1 times the characters. As a result, the time complexity is O (N) (which counts the number of characters in the entire string to traverse once), and the space complexity is O (1) (no matter how many characters there are in the string, only 256 can be created). Of course, we can also use map, the associated container, to count the occurrences of characters in a string, and then find the first character that appears only 1 times. Code implementation:
The code for Method 1 is not implemented because the interviewer is generally not liked in the interview.
Method 2:
Using arrays to store statistical results: Char getfirstnotrepeating (const string& s) {if (s.size () = 0) return ';
int hasharray[256] = {0};
The number of occurrences of the statistic character for (size_t i = 0; i < s.size (); ++i) {hasharray[s[i]]++;
for (size_t i = 0; i < s.size (), i++) {if (hasharray[s[i]] = = 1) return s[i];
Return ' a ';
int main () {string S;
Getline (cin,s);
char ret = getfirstnotrepeating (s);
cout<<ret<<endl;
System ("pause");
return 0;
///Use map to store statistical results: Char getfirstnotrepeating (const string& s) {if (s.size () = 0) return ';
Map<char,int> m;
Map<char,int>::iterator it = M.begin (); for (int i = 0; i < s.size (); ++i) {m[s[i]]++;//operator[], not inserted, then return value reference} for (int i = 0; I & Lt S.size (); ++i) {it = M.find (S[i]);//If found, returns the iterator for the current position.
Otherwise returns Map::end if (It->second = = 1) return s[i];
Return ' a '; int main () {string S;
Getline (cin,s);
char ret = getfirstnotrepeating (s);
cout<<ret<<endl;
System ("pause");
return 0;
}
Related Topics:
1. Deletes all characters that appear in the 2nd string from the first string. For example, enter the first string "You are good", the second string "root", output "Yu ae gd".
Idea Analysis: Create a simple hash table to store the occurrences of the characters in the second string, and then delete the corresponding characters in the first string.
Code implementation:
void Erase (string s1,const string& s2)
{
if (s1.size () = 0 | | s2.size () = 0) return
;
String::iterator it = S1.begin ();
BOOL Hash[256]={false};
for (int i = 0; i < s2.size (); ++i)
{
Hash[s2[i]] = true;
}
for (int i = 0; i < s1.size ();)
{
if (hash[s1[i]] = = True)
s1.erase (it + i);
else
++i;
}
cout<<s1<<endl;
}
int main ()
{
string S1;
string S2;
/*cin>>s1;
cin>>s2;*///try not to write like this
getline (CIN,S1);
Getline (CIN,S2);
Erase (S1,S2);
System ("pause");
return 0;
}
2. Deletes all occurrences of the character in the string. For example, enter "Google", Output "Gole".
IDEA Analysis: Use a hash table to store the characters in the string.
Code implementation:
void eraserepeating (const string& s)
{
if (s.size () = 0) return
;
BOOL Hash[256] = {false};
for (int i = 0; i < s.size (); ++i)
{
Hash[s[i]] = true;
}
string tmp;
for (int i = 0; i < s.size (); ++i)
{
if (hash[s[i]] = = True)
Tmp.push_back (s[i));
Hash[s[i]] = false;
}
cout<<tmp<<endl;
}
int main ()
{
string s;
Getline (cin,s);
Eraserepeating (s);
System ("pause");
return 0;
}
3. To determine whether two words are mutually modified words. The modified word is that if two words appear in the same letter and the number of letters appear the same, then the two words are modified each other.
Thinking analysis: Using a simple hash array to store the letters and occurrences of a word, another word goes directly to the array, a letter appears, minus 1 of the corresponding position, and if the array finally turns to 0, the two words are modified each other.
Code implementation:
BOOL Isanagram (const string& s1,const string& s2)
{
if (s1.size () = 0 | | s2.size () = 0) return
true;< C3/>int hash[256] = {0};
for (int i = 0; i < s1.size (); ++i)
{
hash[s1[i]]++;
}
for (int i = 0; i < s2.size (); ++i)
{
hash[s2[i]]--;
}
for (int i = 0; i < 256 ++i)
{
if (Hash[i]!= 0) return
false;
}
return true;
}
int main ()
{
string S1;
string S2;
Getline (CIN,S1);
Getline (CIN,S2);
BOOL ret = Isanagram (S1,S2);
cout<<ret<<endl;
System ("pause");
return 0;
}
Summarize:
1. When entering a string, try not to use CIN, because the spaces, tabs, and so on in the string are ignored when you enter a string. Instead of using the getline () function, the getline () function uses the method:
string S;
Getline (cin,s);
2. Note When using the String::erase () function: Erase () will return to the next location of the deleted element, so the iterator does not need to move backwards when the deletion succeeds.
3.map operator[], inserted if the current key value map does not exist, otherwise returns a reference to the value value of the current element.
About looking for the first occurrence of only 1 characters, originally thought is very good to write the topic, did not think the simple topic but caused a lot of problems, very rewarding ~ ~ If there is a problem, please leave a message ~