forked from tech-cow/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
242.py
68 lines (54 loc) · 1.23 KB
/
242.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# HashTable方法1
def isAnagram1(self, s, t):
hash1 = {}
hash2 = {}
for char in s:
if char in hash1:
hash1[char] += 1
else:
hash1[char] = 1
for char in t:
if char in hash2:
hash2[char] += 1
else:
hash2[char] = 1
return hash1 == hash2
# HashTable方法2
def isAnagram2(self, s, t):
hash1 = {}
hash2 = {}
for char in s:
hash1[char] = hash1.get(char, 0 ) + 1
for char in t:
hash2[char] = hash2.get(char, 0) + 1
return hash1 == hash2
# HashTable方法3 利用Unicode,创建2个 Fixed Size Array
def isAnagram3(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
hash1 = [0]*26
hash2 = [0]*26
for char in s:
hash1[ord(char) - ord('a')] += 1
for char in t:
hash2[ord(char) - ord('a')] += 1
return hash1 == hash2
# HashTable方法4 利用Unicode,创建1个 Fixed Size Array
def isAnagram4(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
arr = [0]*26
for char in s:
arr[ord(char) - ord('a')] += 1
for char in t:
arr[ord(char) - ord('a')] -= 1
for num in arr:
if num is not 0:
return False
return True