Skip to content

Commit 8ace3fb

Browse files
committed
feat: implement isAnagram function to check if two strings are anagrams
1 parent a93291c commit 8ace3fb

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from collections import Counter
2+
3+
class Solution:
4+
def isAnagram(self, s: str, t: str) -> bool:
5+
"""
6+
s์™€ t๊ฐ€ ์•„๋‚˜๊ทธ๋žจ์ธ์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜.
7+
์•„๋‚˜๊ทธ๋žจ์€ ๋ฌธ์ž์—ด์˜ ๋ฌธ์ž๋“ค์ด ๋™์ผํ•˜์ง€๋งŒ ์ˆœ์„œ๊ฐ€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ๋ฅผ ๋งํ•จ. ๋Œ€์†Œ๋ฌธ์ž๋Š” ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์ง€๋งŒ, ์–ธ์–ด๋งˆ๋‹ค์˜ ํŠน์ˆ˜ ๊ธฐํ˜ธ ๋“ฑ์€ ๊ณ ๋ คํ•ด์•ผ ํ•จ.
8+
9+
๋ฐฉ๋ฒ•:
10+
1. s์™€ t๋ฅผ ์ •๋ ฌํ•˜์—ฌ ๋น„๊ต; o(nlogn) ์‹œ๊ฐ„๋ณต์žก๋„
11+
2. s์™€ t์˜ ๊ฐ ๋ฌธ์ž์˜ ๋นˆ๋„์ˆ˜๋ฅผ ์„ธ์–ด ๋น„๊ต; o(n) ์‹œ๊ฐ„๋ณต์žก๋„
12+
-> ์‹ค์ œ dict๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ๋ณด๋‹ค Counter๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด o(n)์—์„œ๋„ ์•ฝ๊ฐ„ ๋น ๋ฅด๊ฒŒ ๊ตฌํ˜„ ๊ฐ€๋Šฅ
13+
14+
* ์–ธ์–ด๋งˆ๋‹ค์˜ ํŠน์ˆ˜ ๋ฌธ์ž/๊ธฐํ˜ธ๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•ด, casefold()๋ฅผ ์‚ฌ์šฉ.
15+
16+
Args:
17+
s (str): ๋น„๊ตํ•  ๋ฌธ์ž์—ด 1
18+
t (str): ๋น„๊ตํ•  ๋ฌธ์ž์—ด 2
19+
20+
Returns:
21+
bool: s์™€ t๊ฐ€ ์•„๋‚˜๊ทธ๋žจ์ด๋ฉด True, ์•„๋‹ˆ๋ฉด False
22+
"""
23+
return Counter(s.casefold()) == Counter(t.casefold())

0 commit comments

Comments
ย (0)