我需要找到所有可能的最长回文衍生字符串?
我得到的只有一个:
herp = ""
def isPalindrome(s):
if s == s[::-1] :
return True
s = input()
for idy, item in enumerate(s):
for idx, item in enumerate(s):
derp = s[idy:idx+1]
if isPalindrome(derp) and (len(derp) > len(herp)):
herp = derp
print(herp)
例如。字符串是NITIN
输出 : INTNI NITIN
根据示例:“INTNI”是“NITIN”的排列,因此您首先必须获得所有排列,然后检查哪些是回文。
因此,使用您的“isPalindrome”功能,您可以尝试使用以下方法:
from itertools import permutations
def getPalindromes(stringVar):
result = set()
perms = [''.join(p) for p in permutations(stringVar)]
for perm in perms:
if isPalindrome(perm):
result.add(perm)
return result
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句