Leetcode | | 49, Anagrams

Source: Internet
Author: User
Tags value store

Problem:

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

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

Hide TagsHash Table StringTest instructions: Given the extra two sets of strings, find all of the strings that meet the following criteria: (1) The character type of the string is the same as the corresponding number (2) the position of each character is not differentiated

Thinking:

(1) It is very straightforward to think of using hash table to solve this kind of problem

(2) can also borrow STL map, more convenient, primary key key to store a sorted String,value store pointer to the string

Map<string, vector<const string *> > Mapanagram;

Code

Class solution{    public:vector<string> anagrams (vector<string> &strs) {    vector<string > ret;    Map<string, vector<const string *> > Mapanagram;        for (Vector<string>::const_iterator it = Strs.begin ();        It! = Strs.end ();        ++it)    {        string key (*it);                Sort (Key.begin (), Key.end ());        Mapanagram[key].push_back (&*it);    }        For (map<string, vector<const string *> >::const_iterator it = Mapanagram.begin ();        It! = Mapanagram.end ();        ++it)    {        if (it->second.size () > 1)        {for            (Vector<const string *>::const_iterator Itstr = It->second.begin ();                Itstr! = It->second.end ();                ++ITSTR)            {                ret.push_back (**ITSTR);}}    }        return ret;}};


Leetcode | | 49, Anagrams

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.