字符串的字串,就是和顺序相关的。
子序列和顺序没有关系,对于一个字符来说可以要也可以不要。
- 字串的实现
class Substring(object):
"""字串"""
def substring(self, str):
length = len(str)
res = []
for i in range(length):
string = ""
for j in range(i, length):
string += str[j]
res.append(string)
return res
- 子序列的代码实现
class Subsequence(object):
def substring(self, string):
"""子序列"""
res = set()
def sub(string, index, path):
if index == length:
if path:
res.add(path)
return res
sub(string, index + 1, path) # 不要当前位置的字符
yes = path + string[index] # 要当前位置的字符
sub(string, index + 1, yes)
sub(string, 0, "")
return res



