[CodeKata] -18

김가람휘·2022년 3월 8일
0

CodeKata

목록 보기
18/28

def groupAnagrams(strs):
    word = {}

    for i in strs:
        key = ''.join(sorted(i))

        if key not in word:
            word[key] = []
        word[key].append(i)

    result = []
    for key in word:
        result.append(word[key])

    return result
def groupAnagrams(strs):
    result = {}

    for word in sorted(strs):
        key = tuple(sorted(word))
        result[key] = result.get(key, []) + [word] 

    return result.values()
def groupAnagrams(strs):
    result = {}

    for word in sorted(strs):
        key = tuple(sorted(word))
        print(key)
        result[key] = result.get(key, []) + [word] # result[key]가 가지고 있는 value + word
        print(result)

    return result.values()

strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
groupAnagrams(strs)

# ('a', 'e', 't')
# {('a', 'e', 't'): ['ate']}
# ('a', 'b', 't')
# {('a', 'e', 't'): ['ate'], ('a', 'b', 't'): ['bat']}
# ('a', 'e', 't')
# {('a', 'e', 't'): ['ate', 'eat'], ('a', 'b', 't'): ['bat']}
# ('a', 'n', 't')
# {('a', 'e', 't'): ['ate', 'eat'], ('a', 'b', 't'): ['bat'], ('a', 'n', 't'): ['nat']}
# ('a', 'n', 't')
# {('a', 'e', 't'): ['ate', 'eat'], ('a', 'b', 't'): ['bat'], ('a', 'n', 't'): ['nat', 'tan']}
# ('a', 'e', 't')
# {('a', 'e', 't'): ['ate', 'eat', 'tea'], ('a', 'b', 't'): ['bat'], ('a', 'n', 't'): ['nat', 'tan']}

0개의 댓글