File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ # Approach
3+ ๋ฌธ์์ด s๋ฅผ ๊ตฌ์ฑํ๋ ๋ฌธ์์ ๊ฐ์๋ฅผ ์ธ๋ ๋์
๋๋ฆฌ word_dict๋ฅผ ๋ง๋ค๊ณ ,
4+ ๋ฌธ์์ด t๋ฅผ ์ํํ๋ฉฐ word_dict์ ํค๊ฐ ์๋์ง ํ์ธํฉ๋๋ค.
5+ ํค๊ฐ ์์ผ๋ฉด: ์๋๊ทธ๋จ ๋ถ๊ฐ๋ฅ.
6+ ํค๊ฐ ์์ผ๋ฉด: ๊ฐ์์ -1ํ๊ณ , ์์์ธ์ง ํ์ธํ์ฌ ์๋๊ทธ๋จ ์ฌ๋ถ๋ฅผ ํ๋ณํฉ๋๋ค.
7+
8+ # Complexity
9+ - Time complexity: s์ ๊ธธ์ด๊ฐ N์ด๊ณ , t์ ๊ธธ์ด๊ฐ M์ผ ๋ O(N+M)
10+
11+ - Space complexity: O(N+M)
12+ """
13+
14+ from collections import defaultdict
15+
16+
17+ class Solution :
18+ def isAnagram (self , s : str , t : str ) -> bool :
19+ if len (s ) != len (t ):
20+ return False
21+
22+ word_dict = defaultdict (int )
23+ for ch in s : # O(N)
24+ word_dict [ch ] += 1
25+
26+ for ch in t : # O(M)
27+ if ch not in word_dict :
28+ return False
29+ word_dict [ch ] -= 1
30+ if word_dict [ch ] < 0 :
31+ return False
32+
33+ return True
You canโt perform that action at this time.
0 commit comments