1 parent a1da346 commit f15acb9Copy full SHA for f15acb9
1 file changed
LeetCode/Easy/0242-valid-anagram/0242-valid-anagram.py
@@ -1,11 +1,21 @@
1
class Solution:
2
def isAnagram(self, s: str, t: str) -> bool:
3
- # sort_s = sorted(s.lower())
4
- # sort_t = sorted(t.lower())
+ # 1. sort
+ return sorted(s) == sorted(t)
5
6
- # print(sort_s)
7
- # print(sort_t)
+ # 2. hash table
+ hash_table = {}
8
9
- # return sort_s == sort_t
10
-
11
- return sorted(s) == sorted(t)
+ for ch in s:
+ if ch not in hash_table:
+ hash_table[ch] = 0
12
+ hash_table[ch] += 1
13
+
14
+ for ch in t:
15
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