Skip to content

Commit 1e8d5cf

Browse files
committed
FEAT : valid-anagram solution
1 parent fde8195 commit 1e8d5cf

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

β€Žvalid-anagram/sungjinwi.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'''
2+
풀이 :
3+
λ¬Έμžμ—΄ sμ—μ„œ λ¬Έμžκ°€ λ‚˜μ˜¬ λ•Œλ§ˆλ‹€ λ”•μ…”λ„ˆλ¦¬μ— μ €μž₯ν•˜κ³  숫자 증가
4+
λ¬Έμžμ—΄ tμ—μ„œ λ™μΌν•œ λ¬Έμžκ°€ λ‚˜μ˜¬ λ•Œλ§ˆλ‹€ 숫자 κ°μ†Œμ‹œν‚€κ³  0되면 λ”•μ…”λ„ˆλ¦¬μ—μ„œ 제거
5+
λ”•μ…”λ„ˆλ¦¬μ— μ—†λŠ” λ¬Έμžκ°€ λ‚˜μ˜€κ±°λ‚˜ μž‘μ—…μ΄ λλ‚œ ν›„ λ”•μ…”λ„ˆλ¦¬κ°€ λΉ„μ–΄μžˆμ§€ μ•Šλ‹€λ©΄ False
6+
7+
TC :
8+
forλ¬Έ λ‘λ²ˆ 돌기 λ•Œλ¬Έμ— O(N)
9+
10+
SC :
11+
λ”•μ…”λ„ˆλ¦¬ ν• λ‹Ήν•˜λŠ” λ©”λͺ¨λ¦¬λ₯Ό κ³ λ €ν•˜λ©΄ O(N)
12+
'''
13+
14+
class Solution:
15+
def isAnagram(self, s: str, t: str) -> bool:
16+
if len(s) != len(t) :
17+
return (False)
18+
dic = {}
19+
for char in s :
20+
if char in dic :
21+
dic[char] += 1
22+
else :
23+
dic[char] = 1
24+
for char in t :
25+
if char in dic :
26+
dic[char] -= 1
27+
if dic[char] == 0 :
28+
dic.pop(char)
29+
else :
30+
return (False)
31+
if dic :
32+
return (False)
33+
else :
34+
return (True)

0 commit comments

Comments
Β (0)