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

后端 未结 15 1882
你的背包
你的背包 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 09:52

    Here's a code you can use for finding the longest palindromic substring:

    string = "sensmamstsihbalabhismadamsihbala"
    string_shortener = ""
    pres = 0
    succ = 3
    p_temp=0
    s_temp=0
    longest = ""
    for i in range(len(string)-2):
        string_shortener = string[pres:succ]
        if(string_shortener==string_shortener[::-1]):
           p_temp = pres
           s_temp = succ
           for u in range(1000):
               p_temp-=1
               s_temp +=1
               string_shortener = string[p_temp:s_temp]
               if(string_shortener == string_shortener[::-1]):
                    if len(string_shortener)>len(longest):
                        longest = string_shortener
                else:
                    break
        pres+=1
        succ+=1
    print(longest)
    

提交回复
热议问题