深信服23届秋招测试开发A卷——子串处理

字符串过滤

对于输入的字符串,从左到右扫描字符串,如果存在由三个以上(包括三个)连续相同字符组成的子串,就将这个子串从原串中去掉,并将原有字符串剩下的部分拼接到一起。重复上述过程,直到无法去掉任何子串。

时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 256M,其他语言512M

示例1

输入例子:

"222BCC111CB"

输出例子:

"BB"

本题可以采用设置一个字符子串开始索引,以便在结果子串中加入满足条件的子串,类似条件预判的方式。然后在经过确认是否满足条件来决定是否继续删除子串。

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param st string字符串 
# @return string字符串
#
class Solution:
    def get_substr(self , st ):
        # write code here
        result=[st]
        while not isok(result[-1]):
            result.append(deletesub(result[-1]))
        return result[-1]
        
    

def deletesub(string):
    resstring=''
    counter=1
    nowchlist=[]
    nowindex=0
    for i in range(len(string)):
        if i==0:
            nowchlist.append(string[0])
        elif string[i]==nowchlist[-1]:
            counter+=1
            if i==len(string)-1:
                if counter>=3:
                    break
                else:
                    resstring+=string[nowindex:]
        elif string[i]!=nowchlist[-1]:
            if counter>=3:
                nowindex=i
                nowchlist.append(string[i])
                counter=1
                if i==len(string)-1:
                    if counter>=3:
                        break
                    else:
                        resstring+=string[nowindex:]
            else:
                if i==len(string)-1:
                    resstring+=string[nowindex:]
                else:
                    resstring+=string[nowindex:i]
                nowchlist.append(string[i])
                nowindex=i
                counter=1
    return resstring

def isok(string):
    nowchlist=[]
    nowindex=0
    counter=1
    for i in range(len(string)):
        if i==0:
            nowchlist.append(string[0])
        elif string[i]==nowchlist[-1]:
            counter+=1
            if i==len(string)-1:
                if counter>=3:
                    return False
                else:
                    return True
        elif string[i]!=nowchlist[-1]:
            if counter>=3:
                return False
            else:
                nowchlist.append(string[i])
                nowindex=i
                counter=1
    return True
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值