Group Anagrams - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        d = defaultdict(list)
        for word in strs:
            key = ''.join(sorted(word))
            d[key].append(word)
 
        print(d.values())
        return list(d.values())
 
 
= ['eat''tea''tan''ate''nat''bat']
= Solution()
f.groupAnagrams(k)
cs

1.

https://02082306.tistory.com/78 3번 참고

defaultdict(list)를 활용했다.

 

2.

dict형과 마찬가지로 str형 또한 sorted()에 넣으면 list로 변환되기 때문에 편하게 사용할 수 있다.

sort() 보다는 sorted()를 더 많이 써야겠다.

'알고리즘 문제풀이 with 파이썬 > LeetCode' 카테고리의 다른 글

[배열] 3Sum  (0) 2021.09.09
[배열] Trapping Rain Water  (0) 2021.09.05
[배열] Two Sum  (0) 2021.09.04
[문자열] Longest Palindromic Substring  (0) 2021.09.03
[문자열] Valid Palindrome  (0) 2021.08.30

+ Recent posts