입력
["eat", "tea", "tan", "ate", "nat", "bat"]
출력
[
["ate", "eat", "tea"],
["nat", "tan"],
["bat"]
]
애너그램이란
일종의 언어유희로 문자를 재배열하여 다른 뜻을 가진 단어로 바꾸는 것을 말한다.
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagrams = collections.defaultdict(list)
for word in strs:
# 정렬하여 딕셔너리에 추가
anagrams[''.join(sorted(word))].append(word)
return list(anagrams.values())
dic = collections.defaultdict(생성자)dic 을 생성한다.dic 에 존재하지 않는 키를 삽입해도 KeyError가 나지 않고 항상 디폴트를 생성한다.'구분자'.join(리스트)dictionary[key].append(value) : 딕셔너리의 해당 키에 값을 추가한다.dictionary.values() : 딕셔너리의 값만으로 리스트를 만든다.