"Leetcode" anagrams

Source: Internet
Author: User

Anagrams

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

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

First explain what is anagrams: a string consisting of the same letter, regardless of order, anagrams

For example:

1, {"Eat", "ate", "tea", "coffee"} anagrams is {"Eat", "ate", "tea"}

2. {"Tea", "and", "ate", "eat", "Dan"} are anagrams {"Tea", "ate", "eat", "and", "Dan"}

Solution one: Sort as key

In the concrete implementation process, two mapping tables m and exist are constructed.

M is used to quickly find a sorted word, and the value is the first subscript of the encoded word.

exist is used to record whether the sorted word already exists in the anagrams,

If so, just load the current word into anagrams;

If not, first load the first word in M that represents the sorted word into anagrams, and then load the current word.

classSolution { Public: Vector<string> anagrams (vector<string> &STRs) {Map<string,int>m; Map<string,BOOL>exist; Vector<string>ret;  for(inti =0; I < strs.size (); i + +)        {            stringCur =Strs[i];                        Sort (Cur.begin (), Cur.end ()); if(M.find (cur) = =m.end ()) M[cur]=i; Else            {                if(Exist.find (cur) = =Exist.end ()) {//if not exist, add the first wordExist[cur] =true;                Ret.push_back (Strs[m[cur]);            } ret.push_back (Strs[i]); }        }        returnret; }};

Solution two: Using mass factor decomposition

Background: Any positive integer can be decomposed into a unique mass factor product: n=2a3b5c7d ...

So n can be represented by {a,b,c,d}.

For this question, we a~z the number of occurrences of each word as a power to each factorization,

You can omit the alphabetical order by uniquely encoding the product. The same coded words make up the anagrams.

Because there are a total of 26 lowercase letters, we need the first 26 qualitative factors.

In the implementation process, I also constructed two mapping tables m and exist

M is used to quickly find the encoding, and the value is the first subscript of the encoded word.

exist is used to record whether the encoding already exists in the anagrams,

If so, just load the current word into anagrams;

If not, first load the first word in M that represents the encoding into anagrams, and then load the current word.

classSolution { Public: Vector<string> anagrams (vector<string> &STRs) {Vector<Long Long int> Code (strs.size (),0);        Calcode (STRs, code); Map<Long Long int,int>m; Vector<string>ret;  for(inti =0; I < strs.size (); i + +)        {            if(M.find (code[i]) = =m.end ()) M[code[i]=i; Else{ret.push_back (strs[m[code[i]]);                Ret.push_back (Strs[i]);  for(intj = i+1; J < Strs.size (); J + +)                {                    if(Code[j] = =Code[i])                    {Ret.push_back (strs[j]); }                }                returnret; }        }        //Not found        returnret; }    voidCalcode (vector<string> &strs, vector<Long Long int> &code) {         for(inti =0; I < strs.size (); i + +)        {            stringCur =Strs[i]; Vector<int> Count ( -,0);//a~z Count             for(intj =0; J < Cur.size (); J + +) {Count[cur[j]-'a'] ++; }            Doubleprime[ -] = {2,3,5,7, One, -, -, +, at, in, to,Panax Notoginseng, A, +, -, -, -, A, the, in, the, -, the, the, the,101}; Long Longresult =1;  for(intj =0; J < -; J + +) {result*= (Long Long int) Pow (prime[j], count[j]); } Code[i]=result; }    }};

"Leetcode" 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.