File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution (object ):
2+ def isAnagram (self , s , t ):
3+ """
4+ :type s: str
5+ :type t: str
6+ :rtype: bool
7+
8+ s์ t์ ๊ธธ์ด๊ฐ ๊ฐ๋ค๋ ์ ์ ์๋
9+ s์ t๋ฅผ ๊ฐ๊ฐ ๋์
๋๋ฆฌ์ ์ ์ฅํ๊ณ
10+ ๋์
๋๋ฆฌ๋ผ๋ฆฌ ๋น๊ตํ์ฌ ์ ๋๊ทธ๋จ ์ฌ๋ถ๋ฅผ ๊ฒ์ฌํ๋ ํจ์.
11+
12+ - ์๊ฐ ๋ณต์ก๋: O(n)
13+ - ๊ณต๊ฐ ๋ณต์ก๋: O(n)
14+ ์ํ๋ฒณ ์๋ฌธ์๋ก ํ์ ํ ๊ฒฝ์ฐ O(1)๋ก ๋ณผ ์๋ ์์ง๋ง
15+ ์ถ๊ฐ ์ฌํญ์ธ UNICODE ๋ฌธ์๊ฐ ์
๋ ฅ๋ ๊ฒฝ์ฐ๋ฅผ ๊ณ ๋ คํ์ฌ
16+ ๊ณต๊ฐ ๋ณต์ก๋๋ฅผ O(n)์ผ๋ก ๊ณ์ฐํจ.
17+
18+ """
19+ # ๋จผ์ s์ t์ ๊ธธ์ด ๋น๊ต
20+ # s์ t๊ฐ ์ ๋๊ทธ๋จ์ด๋ผ๋ฉด ๊ธธ์ด๊ฐ ๊ฐ์
21+ if len (s ) != len (t ):
22+ return False
23+
24+ # ์
๋ ฅ ๋ฐ์ s์ ๊ฐ ๋ฌธ์๋ฅผ ํค, ๋น๋๋ฅผ ๊ฐ์ผ๋ก ํ๋ ๋์
๋๋ฆฌ
25+ sdict = {}
26+
27+ # s์ ๊ฐ ๋ฌธ์๋ฅผ ์ํํ๋ฉด์ sdict ๊ตฌ์ถ
28+ # O(n) ์๊ฐ ์์
29+
30+ for char in s :
31+ if char in sdict :
32+ sdict [char ] += 1
33+ else :
34+ sdict [char ] = 1
35+
36+ # ์
๋ ฅ ๋ฐ์ t์ ๊ฐ ๋ฌธ์๋ฅผ ํค, ๋น๋๋ฅผ ๊ฐ์ผ๋ก ํ๋ ๋์
๋๋ฆฌ
37+ tdict = {}
38+
39+ # t์ ๊ฐ ๋ฌธ์๋ฅผ ์ํํ๋ฉด์ tdict ๊ตฌ์ถ
40+ # O(n) ์๊ฐ ์์
41+
42+ for char in t :
43+ if char in tdict :
44+ tdict [char ] += 1
45+ else :
46+ tdict [char ] = 1
47+
48+ # Python์ ํค์ ์์์ ์๊ด ์์ด ๋์
๋๋ฆฌ๋ผ๋ฆฌ ๋ฐ๋ก ๋น๊ต ๊ฐ๋ฅ
49+ # sdict์ tdict ๋น๊ต ํ ๊ฐ์ผ๋ฉด True ๊ฐ์ง ์์ผ๋ฉด False ๋ฐํ
50+ # ๋์
๋๋ฆฌ ์์ ํค์ ์๊ฐ k์ด๊ณ ๋ชจ๋ ๋ฌธ์๊ฐ ๊ฐ๋ณ์ ์ด๋ผ๋ฉด,
51+ # ์๊ฐ์ O(k)๊ฐ ํ์
52+ # ์ฌ๊ธฐ์ k๋ O(n) ์์ค์ด๋ฏ๋ก ์ ์ฒด ์๊ฐ ๋ณต์ก๋๋ O(n)
53+
54+ if sdict == tdict :
55+ return True
56+ else :
57+ return False
You canโt perform that action at this time.
0 commit comments