Description
给定一个字符串 s
,找出其中所有不含重复字符的子串,并将它们按照字典序排序后输出。
Format
Input
s = "abcabcbb"
Output
['a', 'ab', 'abc', 'b', 'bc', 'bca', 'c', 'ca', 'cab', 'cb']
解释:'abca'虽然为子串,含有重复字符,则不符合要求。
注意:
0 <= s.length <=10
s
仅由小写英文字母组成。s
为空,则输出[]即可。
def find_unique_substrings(s):
if not s:
return []
n = len(s)
unique_substrings = set()
for i in range(n):
for j in range(i, n):
substring = s[i:j+1]
if len(set(substring)) == len(substring):
unique_substrings.add(substring)
return sorted(list(unique_substrings))
# 输入字符串
s = input()
# 查找不含重复字符的子串并按字典序排序
result = find_unique_substrings(s)
print(result)