Skip to content

Commit c63d0ca

Browse files
committed
[mand2] ์ˆ˜์ •: anagram, two-sum
- two-sum: ์“ธ๋ชจ์—†๋Š” ๋กœ๊ทธ ์‚ญ์ œ - anagram: - ์ดˆ๊ธฐํ™” ์ˆ˜์ • - ๋กœ์ง ์ถ”๊ฐ€
1 parent 59ba05e commit c63d0ca

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

โ€Žtwo-sum/mand2.pyโ€Ž

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff 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]

โ€Žvalid-anagram/mand2.pyโ€Ž

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
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

2726
tc_1 = is_anagram("anagram", "nagaram") is True
2827
tc_2 = is_anagram("rat", "car") is False
2928
tc_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)

0 commit comments

Comments
ย (0)