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

后端 未结 15 1889
你的背包
你的背包 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:11

    inputStr = "madammmdd"
    outStr = ""
    uniqStr = "".join(set(inputStr))
    flag = False
    for key in uniqStr:
       val = inputStr.count(key)
       if val % 2 !=0:
          if not flag:
             outStr = outStr[:len(outStr)/2]+key+outStr[len(outStr)/2:]
             flag=True
          val-=1
       outStr=key*(val/2)+outStr+key*(val/2)
    print outStr
    

提交回复
热议问题