1. What I learned
- ’‘.join(sorted(STR))
- With only sorted(STR), each alphabet of STR is sorted and returned in the form of a list. ‘join()’ has to be used to sort the alphabets and merge them back into the string.
2. Code
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
ana_dic = dict()
for e in strs:
temp = ''.join(sorted(e))
if temp in ana_dic:
ana_dic[temp].append(e)
else:
ana_dic[temp] = [e]
return list(ana_dic.values())
3. Result
Runtime : 88 ms(97.70%), Memory usage : 17.2 MB(86.46%)
(Runtime can be different by a system even if it is a same code.)