Problem Definition:
Given an array of strings, return all groups of strings that is anagrams.
Note:all inputs'll is in lower-case.
Solution:
Requires that all existing anagram strings be placed in an array, and all returned results should look like this:
[' EMS ', ' MSE ', ' app ', ' PPA ' ...]
It is easy to achieve with hasptable.
1 #@param {string[]} STRs2 #@return {string[]}3 defanagrams (STRs):4 ifLen (STRs) <=1:5 return []6bigdix={}7 forAStrinchSTRs:8key=Str (sorted (ASTR))9 ifKey not inchBigdix:Tenbigdix[key]=[ASTR] One Else: Abigdix[key]+=AStr, -res=[] - forArrinchbigdix.values (): the ifLen (arr) >1: -res+=arr - returnRes -
It is important to note that the dict in Python cannot guarantee the order of key. ordereddict is saved key-values in the order in which it is inserted, and it can also be used to generate a dictionary sorted by some sort of rule.
leetcode#49 anagrams