File tree Expand file tree Collapse file tree 2 files changed +11
-11
lines changed Expand file tree Collapse file tree 2 files changed +11
-11
lines changed Original file line number Diff line number Diff line change @@ -6,7 +6,6 @@ def solutions(nums, target_num):
6
6
7
7
for i , value in enumerate (nums ):
8
8
look = target_num - value
9
- print ('look:' , look )
10
9
# value -> idx๋ก ๋ฐ๋ก ์นํํ๊ธฐ๊ฐ ์ด๋ ต..
11
10
if look in table and i != table [look ]:
12
11
look_idx = table [look ]
Original file line number Diff line number Diff line change 1
1
# ๋ฌธ์ : https://leetcode.com/problems/valid-anagram/
2
+ from collections import defaultdict
3
+
2
4
3
5
# ํ์ด: s ์ t ์ ์
๋ ฅ๋ ์ํ๋ฒณ์ ๊ฐฏ์๋ฅผ ์ฒดํฌํ๋ค. ...
4
6
# ์ ๋๊ทธ๋จ์ด๋ฉด T
@@ -7,27 +9,26 @@ def is_anagram(s, t) -> bool:
7
9
if len (s ) != len (t ):
8
10
return False
9
11
10
- word_counter = {}
12
+ word_counter = defaultdict ( int )
11
13
# s ๋ฌธ์์ด ๋ถํด
12
14
for alpha in s :
13
- # ์ด๊ธฐํ
14
- if alpha not in word_counter :
15
- word_counter [alpha ] = 0
16
15
word_counter [alpha ] += 1
17
16
18
17
for beta in t :
19
- if beta not in word_counter :
18
+ if beta not in word_counter or word_counter [ beta ] == 0 :
20
19
return False
21
- return True
22
-
20
+ word_counter [ beta ] -= 1
21
+ return True
23
22
24
23
# ์๊ฐ๋ณต์ก๋: O(n)
25
24
# ๊ณต๊ฐ๋ณต์ก๋: O(n)
26
25
27
26
tc_1 = is_anagram ("anagram" , "nagaram" ) is True
28
27
tc_2 = is_anagram ("rat" , "car" ) is False
29
28
tc_3 = is_anagram ("a" , "ab" ) is False
29
+ tc_4 = is_anagram ("aacc" , "ccac" ) is False
30
30
31
- print (tc_1 )
32
- print (tc_2 )
33
- print (tc_3 )
31
+ print ('tc_1' , tc_1 )
32
+ print ('tc_2' , tc_2 )
33
+ print ('tc_3' , tc_3 )
34
+ print ('tc_4' , tc_4 )
You canโt perform that action at this time.
0 commit comments