Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 721bdec

Browse files
committed
feat: add solution for valid anagram in python
1 parent caf07f5 commit 721bdec

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎alternative/easy/valid_anagram.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
3+
def isAnagram(s: str, t: str) -> bool:
4+
5+
if len(s) != len(t):
6+
return False
7+
8+
first = dict()
9+
second = dict()
10+
11+
for c in s:
12+
first[c] = first.get(c, 0) + 1
13+
14+
for c in t:
15+
second[c] = second.get(c, 0) + 1
16+
17+
return first == second
18+
19+
20+
assert isAnagram("anagram", "nagaram") == True
21+
assert isAnagram("rat", "car") == False
22+
assert isAnagram("a", "ab") == False
23+
assert isAnagram("ab", "a") == False
24+
assert isAnagram("ab", "ba") == True

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /