๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

[LeetCode] 49. Group Anagrams

๐Ÿ“๋ฌธ์ œ ํ’€์ด

1. collections.defaultdict()๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋นˆ ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑ ํ›„ append๋กœ ์›์†Œ ์ถ”๊ฐ€

 

class Solution(object):
    def groupAnagrams(self, strs):
        anagrams = collections.defaultdict(list)
        for word in strs:
            sorted_word = ''.join(sorted(word)) 
            anagrams[sorted_word].append(word)
        return list(anagrams.values())