Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Approach:
- 1. sort the values in the list.
- 2. Dictionary
- sorted words will be the key and the words matching these will be values in a list
- 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.
- 4. else append the word to the existing list
- '''
- class Solution:
- def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
- dic = { }
- for words in strs:
- sorted_word = "".join(sorted(words))
- if sorted_word not in dic:
- dic[sorted_word]= [words]
- else:
- dic[sorted_word].append(words)
- return dic.values()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement