"Intermediate algorithm" 49. Alphabetical Grouping of synonyms

Source: Internet
Author: User

Topic:

Given an array of strings, combine the letters with different words. Alphabetic synonyms refer to the same letters but arrange different strings. Example: input: ["Eat", "tea", "tan", "ate", "Nat", "bat"], output: [  "ate", "eat", "tea"],  ["Nat", "Tan"],  ["bat"]] Note: All inputs are lowercase letters. The order in which the answer output is not considered.

Solution:

Class Solution {public:vector<vector<string>> Groupanagrams (vector<string>& strs) {MAP&L        T;string,set<int>> Str_map;                vector<vector<string>> Res;            for (int i = 0;i < Strs.size (); ++i) {string tmp = Strs[i];            Sort (Tmp.begin (), Tmp.end ());            if (Str_map.find (TMP)!=str_map.end ()) {Str_map[tmp].insert (i);                }else{set<int> t;                T.insert (i);            Str_map.insert (Make_pair (tmp,t));        }} Map<string,set<int>>::iterator it = Str_map.begin ();            for (; It!=str_map.end (); ++it) {vector<string> Str_vec;            Set<int>::iterator set_it = (it->second). Begin ();            for (; set_it!= (It->second). end (); ++set_it) {str_vec.push_back (strs[*set_it]);        } res.push_back (Str_vec);    } return res; }};

  

"Intermediate algorithm" 49. Alphabetical Grouping of synonyms

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.