MCPcopy Create free account
hub / github.com/Jack-Lee-Hiter/AlgorithmsByPython / anagramSolution2

Method anagramSolution2

AnagramDetection.py:26–46  ·  view source on GitHub ↗
(self, s1, s2)

Source from the content-addressed store, hash-verified

24 # 计算每个字母出现的次数并存入到相应的list中
25 # 比较两个list是否相同
26 def anagramSolution2(self, s1, s2):
27 c1 = [0] * 26
28 c2 = [0] * 26
29
30 for i in range(len(s1)):
31 pos = ord(s1[i]) - ord('a')
32 c1[pos] = c1[pos] + 1
33
34 for i in range(len(s2)):
35 pos = ord(s2[i]) - ord('a')
36 c2[pos] = c2[pos] + 1
37
38 j = 0
39 stillOK = True
40 while j < 26 and stillOK:
41 if c1[j] == c2[j]:
42 j = j + 1
43 else:
44 stillOK = False
45
46 return stillOK
47
48 # 首先将两个字符串list化
49 # 将两个list中的字符生成两个set

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected