Published:
Updated:


Solution

import collections
from typing import List


class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        answer = []
        dic = collections.defaultdict(list)

        # key: ์ •๋ ฌ๋œ ๋ฌธ์ž์—ด, value: ๊ธฐ์กด ๋ฌธ์ž์—ด ๋ฆฌ์ŠคํŠธ
        # ์ฆ‰, ์ •๋ ฌ๋œ ๋ฌธ์ž์—ด์ธ key๊ฐ’์ด ๊ฐ™์œผ๋ฉด value์— ๊ณ„์† append๊ฐ€ ๋˜๋Š” ํ˜•์‹
        for s in strs:
            dic[''.join(sorted(s))].append(s)

        return list(dic.values())


print(Solution().groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]))

Leave a comment