File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed
Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ '''
2+ Approach 1: Sorting
3+ - ์ ์ผ ๋จผ์ ์๊ฐ๋๋ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ sorted ํจ์๋ฅผ ํ์ฉํด์ ๋ถ๋ฅํด์ s ์ t๊ฐ ๋์ผํ์ง ํ์ธํ๋ ๋ฐฉ๋ฒ์
๋๋ค.
4+ - ์ ๋ ฌ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด n log n์ ์๊ฐ๋ณต์ก๋๊ฐ ๋ฐ์ํ๋ ๊ฒ์ ์ธ์งํ๊ณ ๋ ํ์ด๋ดค์ต๋๋ค.
5+
6+ Time Complexity: O(n log n)
7+ - ๋ถ๋ฅ ์ ๋ ฌ์์ O(n log n) ๋ฐ์
8+
9+ Space Complexity: O(n)
10+ - ๋ถ๋ฅ ๋ฌธ์์ด์ ์์ฑํ๋ ๊ณต๊ฐ ๋๋ฌธ์ O(n) ๋ฐ์
11+ '''
12+ class Solution :
13+ def isAnagram (self , s : str , t : str ) -> bool :
14+ sorted_s = '' .join (sorted (s ))
15+ sorted_t = '' .join (sorted (t ))
16+ if (sorted_s == sorted_t ):
17+ return True
18+ else :
19+ return False
20+
21+ '''
22+ Approach 2: ๋น๋์ ์นด์ดํ
23+ - ๋ ๋ฌธ์์ด์ ๊ธธ์ด๋ฅผ ๋จผ์ ๋น๊ตํด์ ๋ค๋ฅด๋ฉด False๋ฅผ ๋ฐํํด์ 1์ฐจ ํํฐ๋ง
24+ - count ๋์
๋๋ฆฌ๋ฅผ ์์ฑํด์ s ๋ฌธ์์ด์ ๋น๋์๋ฅผ ์นด์ดํ
25+ - t ๋ฌธ์์ด์ ์ํํ๋ฉด์ ๋์
๋๋ฆฌ์ ํฌํจ๋์ด ์์ผ๋ฉด ์นด์ดํ
์ ๊ฐ์์ํค๊ณ , ๊ฐ์ด ์์ผ๋ฉด False๋ฅผ ๋ฐํ
26+
27+
28+ Time Complexity: O(n)
29+ - ๋ ๋ฌธ์์ด์ ๊ฐ๊ฐ ์ํํ๋ฉด ๋์
๋๋ฆฌ ์์ฑ, ๋น๋์ ์นด์ดํ
๋ฐ ๊ฐ์ ์์ O(n) ๋ฐ์
30+
31+ Space Complexity: O(n)
32+ - count ๋์
๋๋ฆฌ ์ ์ฅ ๊ณต๊ฐ ๋๋ฌธ์ O(n) ๋ฐ์
33+ '''
34+ class Solution :
35+ def isAnagram (self , s : str , t : str ) -> bool :
36+ # ๊ธธ์ด๊ฐ ์ผ์นํ์ง ์์ผ๋ฉด False
37+ if len (s ) != len (t ):
38+ return False
39+ # ๋ฌธ์ ๊ฐ์ ์นด์ดํธ
40+ count = {}
41+ for val in s :
42+ count [val ] = count .get (val , 0 ) + 1
43+ for val in t :
44+ if val not in count :
45+ return False
46+ count [val ] -= 1
47+ # ์์ : s = "aacc", t = "ccac" ์ธ ๊ฒฝ์ฐ count[val] ์ด ์์๊ฐ ๋ ์ ์์
48+ if count [val ] < 0 :
49+ return False
50+ return True
You canโt perform that action at this time.
0 commit comments