Python 題目

PROBLEM

找到字串裡面的迴文

一題一題刷

s = input("輸入字串:")
n = len(s)
palindromes = set()

for i in range(n):
    for j in range(i+1, n+1):
        substr = s[i:j]
        if substr == substr[::-1] and len(substr) > 1:
            palindromes.add(substr)

print("所有迴文子字串:", palindromes)