Skip to content

Commit 19018be

Browse files
committed
Solve: Valid anagram
1 parent db88752 commit 19018be

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

โ€Žvalid-anagram/Jay-Mo-99.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
๏ปฟ #ํ•ด์„
2+
#๋ฌธ์ž์—ด s์˜ ์žฌ๋ฐฐ์น˜๋กœ ๋ฌธ์ž์—ด t๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉด anagram์œผ๋กœ return true.
3+
#s์™€ t๋ฅผ list๋กœ ๋ฐ”๊พธ๊ณ  sortํ•˜์—ฌ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ์‹œํ‚จ๋‹ค, ๋‘˜์ด ๊ฐ™์œผ๋ฉด ๊ฐ™์€ ๋ฌธ์ž๋ฅผ ๊ฐ€์ง„ ๋ฌธ์ž์—ด ๋ฆฌ์ŠคํŠธ์ด๋ฏ€๋กœ return true
4+
5+
#Big O
6+
#N: ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด s์™€ t์˜ ๊ธธ์ด(N)
7+
8+
#Time Complexity: O(N)
9+
#- ๋ฌธ์ž์—ด์„ list๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ž‘์—…: O(N)
10+
#- ์ •๋ ฌ์ž‘์—… : NO(log N)
11+
#- ๋ฆฌ์ŠคํŠธ ๋น„๊ต ์ž‘์—…, s์™€ t์˜ ๊ฐ ๋ฌธ์ž๊ฐ€ ์„œ๋กœ ์ผ์น˜ํ•˜๋Š”์ง€ ์ฒดํฌํ•œ๋‹ค : O(N)
12+
#- ์ตœ์ข…: O(N)
13+
14+
#Space Complexity: O(N)
15+
#- list s์™€ t๋Š” ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด s์™€ t์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ์ƒˆ๋กœ์šด list ๊ฐ์ฒด๋กœ ํ• ๋‹น๋œ๋‹ค: O(N)
16+
17+
class Solution(object):
18+
def isAnagram(self, s, t):
19+
"""
20+
:type s: str
21+
:type t: str
22+
:rtype: bool
23+
"""
24+
s = list(s) #Convert string to list
25+
t = list(t)
26+
s.sort() #Sort the list
27+
t.sort()
28+
29+
return s == t #If the s and t are same, return true(anagram)
30+
31+
32+

0 commit comments

Comments
ย (0)