获取字符串中全部的回文子串

获取字符串中全部的回文子串

代码:

def get_all_Palindrome(item:str):
    '''
    获取所有的回文字符串
    '''
    #定义列表,存放所有的回文字符串
    res = []
    for i in range(len(item)):
        for j in range(i + 1, len(item) + 1):
            if item[i:j] == item[i:j][::-1]:
                res.append(item[i:j])
    if res != '':
        return res

测试:

test = "cdabbacc"
print(get_all_Palindrome(test))

结果:

['c', 'd', 'a', 'abba', 'b', 'bb', 'b', 'a', 'c', 'cc', 'c']

 

posted @ 2022-04-19 16:45  未来可期_Durant  阅读(377)  评论(0)    收藏  举报