- Notifications
You must be signed in to change notification settings - Fork 306
/
Copy path242.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
defisAnagram1(self, s, t):
hash1= {}
hash2= {}
forcharins:
ifcharinhash1:
hash1[char] +=1
else:
hash1[char] =1
forcharint:
ifcharinhash2:
hash2[char] +=1
else:
hash2[char] =1
returnhash1==hash2
# HashTable方法2
defisAnagram2(self, s, t):
hash1= {}
hash2= {}
forcharins:
hash1[char] =hash1.get(char, 0 ) +1
forcharint:
hash2[char] =hash2.get(char, 0) +1
returnhash1==hash2
# HashTable方法3 利用Unicode,创建2个 Fixed Size Array
defisAnagram3(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
hash1= [0]*26
hash2= [0]*26
forcharins:
hash1[ord(char) -ord('a')] +=1
forcharint:
hash2[ord(char) -ord('a')] +=1
returnhash1==hash2
# HashTable方法4 利用Unicode,创建1个 Fixed Size Array
defisAnagram4(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
arr= [0]*26
forcharins:
arr[ord(char) -ord('a')] +=1
forcharint:
arr[ord(char) -ord('a')] -=1
fornuminarr:
ifnumisnot0:
returnFalse
returnTrue