File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ https://leetcode.com/problems/valid-anagram/description/
3+ 두 문자열이 애너그램인지 확인하는 함수를 작성하세요.
4+ 애너그램이란 두 문자열이 중복된 알파벳을 같은 개수만큼 포함하고 있는 것을 의미합니다.
5+
6+ TC: O(n)
7+ SC: O(k), k = 알파벳 개수
8+ """
9+
10+ from collections import defaultdict
11+
12+ class Solution :
13+ def isAnagram (self , s : str , t : str ) -> bool :
14+ def makeMap (s : str ):
15+ str_map = defaultdict (int )
16+ for char in s :
17+ str_map [char ] += 1
18+ return str_map
19+
20+ return makeMap (s ) == makeMap (t )
21+
22+
23+ """
24+ 정렬 풀이
25+
26+ TC: O(nlogn)
27+ SC: O(1)
28+ """
29+ def isAnagram (s : str , t : str ) -> bool :
30+ return sorted (s ) == sorted (t )
31+
32+ """
33+ Counter 사용 풀이
34+
35+ TC: O(n)
36+ SC: O(k)
37+ """
38+ from collections import Counter
39+
40+ def isAnagram (s : str , t : str ) -> bool :
41+ return Counter (s ) == Counter (t )
You can’t perform that action at this time.
0 commit comments