python删除三个以上相邻重复字符

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

str1 = "abcccbbdefgggff"
def removeDuplicates(s):
    flag = 1
    while 1:
        original_l = len(s)
        i = 0
        while i < len(s) -1:
            if i >= 0 and s[i] == s[i+1]:
                s = s[:i] + s[i+3:]
                i -= 2
            i += 1
        if original_l == len(s):
            return s

print(removeDuplicates(str1))

你可能感兴趣的:(Python,leetcode,python)