Skip to content

Commit 14544c0

Browse files
Update 242-Valid-Anagrams.py
1 parent 32ccc97 commit 14544c0

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

242-Valid-Anagrams.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
class Solution:
22
def isAnagram(self, s: str, t: str) -> bool:
3-
if len(s) != len(t):
4-
return False
53

6-
countS, countT = {}, {}
4+
if len(s)!=len(t):
5+
return False
76

8-
for i in range(len(s)):
9-
countS[s[i]] = 1 + countS.get(s[i], 0)
10-
countT[t[i]] = 1 + countT.get(t[i], 0)
11-
for c in countS:
12-
if countS[c] != countT.get(c, 0):
7+
char_map = {}
8+
for i in s:
9+
char_map[i]=char_map.get(i,0)+1
10+
11+
for i in t:
12+
if i in char_map:
13+
char_map[i]-=1
14+
15+
16+
for i in char_map.values():
17+
if i!=0:
1318
return False
19+
1420
return True

0 commit comments

Comments
 (0)