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