leetcode_49 problem--anagrams (string,hashtable, algorithm sort, iterator)

Source: Internet
Author: User

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 &GT;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.