给定一个全部由小写英文字母组成的字符串,允许你至多删掉其中 3 个字符,结果可能有多少种不同的字符串?
输入在一行中给出全部由小写英文字母组成的、长度在区间 [4, 106] 内的字符串。
在一行中输出至多删掉其中 3 个字符后不同字符串的个数。
ababcc
25
提示:
删掉 0 个字符得到 "ababcc"。
删掉 1 个字符得到 "babcc", "aabcc", "abbcc", "abacc" 和 "ababc"。
删掉 2 个字符得到 "abcc", "bbcc", "bacc", "babc", "aacc", "aabc", "abbc", "abac" 和 "abab"。
删掉 3 个字符得到 "abc", "bcc", "acc", "bbc", "bac", "bab", "aac", "aab", "abb" 和 "aba"。
代码长度限制
16 KB
- from itertools import combinations
- s = input()
- length = len(s)
- arr = [int(i) for i in range(len(s))]
- temp1 = []
- temp2 = []
- temp3 = []
- x01 = 1+len(s)
- for i in combinations(arr,2):
- i = list(i)
- i.sort()
- if i not in temp2:
- temp2.append(i)
- for i in combinations(arr, 3):
- i = list(i)
- i.sort()
- if i not in temp3:
- temp3.append(i)
- newS1 = []
- newS2 = []
- newS3 = []
- for i in arr:
- st = list(s)
- st[i] = ''
- S = ''.join(st)
- if S not in newS1:
- newS1.append(S)
- for brr in temp2:
- st = list(s)
- for i in brr:
- st[i] = ''
- S = ''.join(st)
- if S not in newS2:
- newS2.append(S)
- for brr in temp3:
- st = list(s)
- for i in brr:
- st[i] = ''
- S = ''.join(st)
- if S not in newS3:
- newS3.append(S)
- total = len(newS1) + len(newS3)+len(newS2)+1
- print(total)