MCPcopy
hub / github.com/Jack-Lee-Hiter/AlgorithmsByPython / group

Method group

Target Offer/字符串的排列和组合.py:30–47  ·  view source on GitHub ↗
(self, ss)

Source from the content-addressed store, hash-verified

28 # 扩展习题, 生成字符的所有组合
29 # 比如输入abc, 则他们的组合有['a', 'ab', 'abc', 'ac', 'b', 'bc', 'c'], ab和ba属于不同的排列, 但属于同一个组合
30 def group(self, ss):
31 if not len(ss):
32 return []
33 if len(ss) == 1:
34 return list(ss)
35 charList = list(ss)
36 charList.sort()
37 pStr = []
38 for i in range(len(charList)):
39 pStr.append(charList[i])
40 if i > 0 and charList[i] == charList[i - 1]:
41 continue
42 temp = self.group(''.join(charList[i + 1:]))
43 for j in temp:
44 pStr.append(charList[i] + j)
45 pStr = list(set(pStr))
46 pStr.sort()
47 return pStr
48
49ss = 'acb'
50S = Solution()

Callers 6

reTest.pyFile · 0.80
addFunction · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected