L3-020 至多删三个字符(Python)

给定一个全部由小写英文字母组成的字符串,允许你至多删掉其中 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)

 

  • 15
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值