Skip to content

Commit 388a7e6

Browse files
committed
valid-anagram solution
1 parent 1f51b19 commit 388a7e6

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

valid-anagram/hyeri0903.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
"""
4+
Determine whether two strings are anagrams.
5+
6+
Time Complexity: O(n)
7+
Space Complexity: O(k)
8+
- n: length of the string
9+
- k: number of unique characters
10+
"""
11+
if len(s) != len(t):
12+
return False
13+
14+
dic_s = {}
15+
dic_t = {}
16+
17+
for i in s:
18+
if i in dic_s:
19+
dic_s[i] += 1
20+
else:
21+
dic_s[i] = 1
22+
23+
for j in t:
24+
if j in dic_t:
25+
dic_t[j] += 1
26+
else:
27+
dic_t[j] = 1
28+
29+
return dic_s == dic_t

0 commit comments

Comments
 (0)