Python: search longest palindromes within a word and palindromes within a word/string

后端 未结 15 1843
你的背包
你的背包 2020-12-03 09:38

So here is a code i have written to find palindromes within a word (To check if there are palindromes within a word including the word itself) Condition: spaces inbetween ch

15条回答
  •  一生所求
    2020-12-03 10:08

    I have made function name as maxpalindrome(s) in this one string argument 's'. This function will return longest possible palindrome sub string and length of substring...

    def maxpalindrome(s):
    if len(s) == 1 or s == '':
        return str(len(s)) + "\n" + s
    else:
        if s == s[::-1]:
            return str(len(s)) + "\n" + s
        else:
            for i in range(len(s)-1, 0, -1):
                for j in range(len(s)-i+1):
                    temp = s[j:j+i]
                    if temp == temp[::-1]:
                        return str(len(temp)) +"\n"+temp
    

提交回复
热议问题