超强回文字符串检测
递归
功能检测回文字符串
语言:Python
代码如下:
def isPalindrome(s):
def toChara(s):
s=s.lower() #将大写转换为小写
ans=""
for c in s :
if c in 'abcdefghijklmnopqrstuvwxyz': #排除空格等字符
ans=ans+c
return ans
def isPal(s):
if len(s)<=1:
return True
else:
return s[0]==s[-1] and isPal(s[1:-1]) #超强递归法
return isPal(toChara(s))
测试用例
isPalindrome(“waaw”)
isPalindrome(“waawaaa”)
isPalindrome(“Able was I ere saw Elba”)