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):
66
77 for i , value in enumerate (nums ):
88 look = target_num - value
9- print ('look:' , look )
109 # value -> idx๋ก ๋ฐ๋ก ์นํํ๊ธฐ๊ฐ ์ด๋ ต..
1110 if look in table and i != table [look ]:
1211 look_idx = table [look ]
Original file line number Diff line number Diff line change 11# ๋ฌธ์ : https://leetcode.com/problems/valid-anagram/
2+ from collections import defaultdict
3+
24
35# ํ์ด: s ์ t ์ ์
๋ ฅ๋ ์ํ๋ฒณ์ ๊ฐฏ์๋ฅผ ์ฒดํฌํ๋ค. ...
46# ์ ๋๊ทธ๋จ์ด๋ฉด T
@@ -7,27 +9,26 @@ def is_anagram(s, t) -> bool:
79 if len (s ) != len (t ):
810 return False
911
10- word_counter = {}
12+ word_counter = defaultdict ( int )
1113 # s ๋ฌธ์์ด ๋ถํด
1214 for alpha in s :
13- # ์ด๊ธฐํ
14- if alpha not in word_counter :
15- word_counter [alpha ] = 0
1615 word_counter [alpha ] += 1
1716
1817 for beta in t :
19- if beta not in word_counter :
18+ if beta not in word_counter or word_counter [ beta ] == 0 :
2019 return False
21- return True
22-
20+ word_counter [ beta ] -= 1
21+ return True
2322
2423# ์๊ฐ๋ณต์ก๋: O(n)
2524# ๊ณต๊ฐ๋ณต์ก๋: O(n)
2625
2726tc_1 = is_anagram ("anagram" , "nagaram" ) is True
2827tc_2 = is_anagram ("rat" , "car" ) is False
2928tc_3 = is_anagram ("a" , "ab" ) is False
29+ tc_4 = is_anagram ("aacc" , "ccac" ) is False
3030
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