-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
AnagramDetection.py
72 lines (59 loc) · 1.89 KB
/
AnagramDetection.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
69
70
71
72
class AnagramDetection:
# 先对两个字符串进行list化
# 对字符串对应的两个list进行排序
# 依次比较字符是否匹配
def anagramSolution1(self, s1, s2):
alist1 = list(s1)
alist2 = list(s2)
alist1.sort()
alist2.sort()
pos = 0
matches = True
while pos < len(s1) and matches:
if alist1[pos] == alist2[pos]:
pos = pos + 1
else:
matches = False
return matches
# 首先生成两个26个字母的list
# 计算每个字母出现的次数并存入到相应的list中
# 比较两个list是否相同
def anagramSolution2(self, s1, s2):
c1 = [0] * 26
c2 = [0] * 26
for i in range(len(s1)):
pos = ord(s1[i]) - ord('a')
c1[pos] = c1[pos] + 1
for i in range(len(s2)):
pos = ord(s2[i]) - ord('a')
c2[pos] = c2[pos] + 1
j = 0
stillOK = True
while j < 26 and stillOK:
if c1[j] == c2[j]:
j = j + 1
else:
stillOK = False
return stillOK
# 首先将两个字符串list化
# 将两个list中的字符生成两个set
# 比较两个set, 如果不相等直接返回false
# 如果两个set相等, 比较每个set中字符在相应list中的个数, 个数不同返回false
def anagramSolution3(self, s1, s2):
alist1 = list(s1)
alist2 = list(s2)
aset1 = set(alist1)
aset2 = set(alist2)
if aset1 != aset2:
return False
else:
for ch in aset1:
if alist1.count(ch) != alist2.count(ch):
return False
return True
s1 = 'abcde'
s2 = 'acbde'
test = AnagramDetection()
print(test.anagramSolution1(s1, s2))
print(test.anagramSolution2(s1, s2))
print(test.anagramSolution3(s1, s2))