Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. '''
  2. Approach:
  3.  
  4. 1. sort the values in the list.
  5. 2. Dictionary
  6.   sorted words will be the key and the words matching these will be values in a  list
  7. 3. if the  sorted word is not present in the dictionary, add the sorted word to the dictionary with     the corresponding word as the value.
  8. 4. else append  the  word  to the existing  list
  9.  
  10. '''
  11.  
  12.  
  13.  
  14. class Solution:
  15.     def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
  16.        
  17.         dic = { }
  18.        
  19.         for words in strs:
  20.             sorted_word  = "".join(sorted(words))
  21.            
  22.             if sorted_word not in dic:
  23.                 dic[sorted_word]= [words]
  24.             else:
  25.                 dic[sorted_word].append(words)
  26.                        
  27.         return dic.values()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement