Skip to content

Commit ea20498

Browse files
valid anagram solution
1 parent b00545f commit ea20498

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from typing import *
2+
3+
4+
class Solution:
5+
"""
6+
ํ’€์ด:
7+
s์™€ t ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅด๋ฉด anagram ์•„๋‹ˆ๋ฏ€๋กœ False๋กœ early return
8+
set์œผ๋กœ s์˜ ์ค‘๋ณต ๋ฌธ์ž ์ œ๊ฑฐ
9+
set์„ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ ๋ฌธ์ž๊ฐ€ s์™€ t์—์„œ ๋“ฑ์žฅํ•˜๋Š” ํšŸ์ˆ˜๊ฐ€ ๋™์ผํ•œ์ง€ ํ™•์ธ
10+
๋‹ค๋ฅด๋ฉด False, ๋๊นŒ์ง€ ํ†ต๊ณผํ•˜๋ฉด True
11+
12+
TC: O(n+m)
13+
- if len(s) != len(t): O(1)
14+
- ss = set(s): O(n)
15+
- for c in ss: ์ตœ๋Œ€ 26ํšŒ ๋ฐ˜๋ณต โ†’ O(1)
16+
- s.count(c): O(n)
17+
- t.count(c): O(m)
18+
- ์ตœ์ข…: O(n+m)
19+
20+
SC: O(n)
21+
- ss = set(s): ์ตœ์•…์˜ ๊ฒฝ์šฐ O(n)
22+
- ๊ทธ ์™ธ ์ƒ์ˆ˜: O(1)
23+
- ์ตœ์ข…: O(n)
24+
"""
25+
def isAnagram(self, s: str, t: str) -> bool:
26+
if len(s) != len(t):
27+
return False
28+
29+
ss = set(s)
30+
for c in ss:
31+
if s.count(c) != t.count(c):
32+
return False
33+
34+
return True

0 commit comments

Comments
ย (0)