Skip to content

Commit f15acb9

Browse files
committed
Time: 15 ms (44.44%), Space: 20.3 MB (10.29%) - LeetHub
1 parent a1da346 commit f15acb9

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
class Solution:
22
def isAnagram(self, s: str, t: str) -> bool:
3-
# sort_s = sorted(s.lower())
4-
# sort_t = sorted(t.lower())
3+
# 1. sort
4+
return sorted(s) == sorted(t)
55

6-
# print(sort_s)
7-
# print(sort_t)
6+
# 2. hash table
7+
hash_table = {}
88

9-
# return sort_s == sort_t
10-
11-
return sorted(s) == sorted(t)
9+
for ch in s:
10+
if ch not in hash_table:
11+
hash_table[ch] = 0
12+
hash_table[ch] += 1
13+
14+
for ch in t:
15+
if ch not in hash_table:
16+
return False
17+
elif hash_table[ch] == 0:
18+
del hash_table[ch]
19+
else:
20+
hash_table[ch] -= 1
21+
return not hash_table

0 commit comments

Comments
 (0)