Skip to content

Commit 2973412

Browse files
committed
solution: valid anagram
1 parent c8870f6 commit 2973412

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

valid-anagram/flynn.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'''
2+
For N, length of the given strings,
3+
4+
Time Complexity: O(N)
5+
- first iteration: O(N)
6+
- second iteration: O(N)
7+
8+
Space Compelxity: O(N)
9+
- dictionaries for each strings: O(N)
10+
'''
11+
12+
class Solution:
13+
def isAnagram(self, s: str, t: str) -> bool:
14+
if len(s) != len(t):
15+
return False
16+
17+
count_s, count_t = {}, {}
18+
for i in range(len(s)):
19+
count_s[s[i]] = count_s.get(s[i], 0) + 1
20+
count_t[t[i]] = count_t.get(t[i], 0) + 1
21+
22+
for c in count_s:
23+
if c not in count_t or count_s[c] != count_t[c]:
24+
return False
25+
return True

0 commit comments

Comments
 (0)