Created
July 13, 2021 14:47
-
-
Save dongwooklee96/1fabcae773f368fd17ffc3f54f7891ca to your computer and use it in GitHub Desktop.
2.4
2.4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| ## 문제 : 2.4 그룹 애너그램 | |
| 문자열 리스트가 주어지는데, 리스트에 있는 문자열을 검사해 서로 같은 애너그램을 가지는 문자열을 그룹으로 묶어보자. | |
| 예를 들어서, 문자열 리스트가 아래와 같이 주어졌다고 가정하자. | |
| strs = ['eat', 'repaid', 'paired', 'tea', 'bat'] | |
| res = [ | |
| ['eat', 'tea'], | |
| ['repaid', 'paired'], | |
| ['bat'] | |
| ] | |
| ## 제한 사항 | |
| 1. 문자열 리스트 | |
| 2. 리스트에 있는 모든 문자열은 소문자로 구성되어 있다. | |
| """ | |
| import collections | |
| from typing import List | |
| def groupAnagrams(strs: List[str]) -> List[List[str]]: | |
| hashmap = collections.defaultdict(list) | |
| for s in strs: | |
| count = [0] * 26 | |
| for ch in s: | |
| count[ord(ch) - ord('a')] += 1 | |
| hashmap[tuple(count)].append(s) | |
| return hashmap.values() | |
| if __name__ == "__main__": | |
| strs = ['eat', 'repaid', 'paired', 'tea', 'bat'] | |
| print(groupAnagrams(strs)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment