AnagramsTotal accepted:33531 Total submissions:137666my submissions QuestionSolution
Given an array of strings, return all groups of strings that is anagrams.
Note:all inputs'll is in lower-case.
Hide TagsHash Table StringHas you met this question in a real interview? Yes No
Discuss
This problem involves a lot of the contents of the templates and containers of C + +, in the subject, the title means to find those strings of letters just like the order of those strings of different
In this case, using a hash table to do, I will be ordered string as the key, and the original string as a value, and then in the hash table to find, count
#include <iostream> #include <string> #include <map> #include <vector> #include <utility> Used the pair's header file #include<algorithm>//to sort, the header file using namespace std;vector<string> anagrams (vector <string> &strs) {multimap<string,string> temp;vector<string> result_str;string str_temp; String Str_temp2;int i=0;int len=strs.size (); while (I<len) {Str_temp=strs[i];str_temp2=strs[i];sort (str_ Temp.begin (), Str_temp.end ());//Sort the string//temp.insert (pair<string,string> (Str_temp,strs)); Temp.insert ( Make_pair (STR_TEMP,STR_TEMP2));//string in order to do the key value, the original value//temp.insert (Multimap<string,string>::value_type (str_ Temp,strs)); i++;} For (Multimap<string,string>::iterator Itr=temp.begin (); Itr!=temp.end (); itr++) {if (Temp.count (Itr->first >1)//To find all the number of key values extra 1, the value of the output result_str.push_back (Itr->second);} return result_str; }int Main () {string str1= "asdf"; string str2= "FDAs"; string str3= "DHFZXCX"; string str4= "Etbd";vector<string> STRs ; Strs.push_back (STR1); Strs.push_back (str2); Strs.push_back (STR3); Strs.push_back (STR4);vector<string> Strs1;strs1=anagrams ( STRs); int i=0;int len=strs1.size (); while (I<len) {cout<<strs1[i]<<endl;i++;} System ("pause"); return 1;}
leetcode_49 problem--anagrams (string,hashtable, algorithm sort, iterator)