"Anagrams" CPP

Source: Internet
Author: User

Topic:

Given an array of strings, return all groups of strings that is anagrams.

Note:all inputs'll is in lower-case.

Code:

classSolution { Public: Vector<string> anagrams (vector<string>&STRs) {Vector<string>ret; Map<string,vector<int> >Str_indexs; //Trans STRs to key (sorted string) and value (Indexs of STRs, that has same key) pairs         for(size_t i =0; I < strs.size (); ++i) {stringKey =Strs[i];            Std::sort (Key.begin (), Key.end ());        Str_indexs[key].push_back (i); }        //Add the keys which occurs more than once         for(map<string,vector<int> >::iterator it = str_indexs.begin (); It!= Str_indexs.end (); ++it) {            if(It->second.size () >1 )            {                 for(size_t k =0; K < It->second.size (); ++k) {Ret.push_back (Strs[it-Second[k]]); }            }        }        returnret; }};

Tips

The use of Std::map is not familiar, and then the problem is familiar.

This problem is very classic, direct search of AC code. Learn a bit, and read a few blogs recorded below.

Http://bangbingsyb.blogspot.sg/2014/11/leetcode-anagrams.html

Http://www.cnblogs.com/AnnieKim/archive/2013/04/25/3041982.html

"Anagrams" CPP

Related Article

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.