[本文出自天外归云的博客园]
问题:找出字符串中最长回文子串
我的想法是:抛砖引玉。找出所有的子串,逐一判断它们是否为回文,并记录最长的回文子串。
[En]
My idea: throw a brick to attract jade. Find out all the substrings, determine whether they are palindromes one by one, and record the longest palindromic substring.
代码如下:
#!/usr/bin/python
def is_huiwen(s):
low, high = 0, len(s)-1
while low < high:
if s[low] != s[high]:
return False
low += 1
high -= 1
return True
def get_longest_huiwen_sub(a):
sub_longest = ""
max_length = 0
for i in range(len(a)):
for j in range(i, len(a)):
if j - i + 1 max_length:
continue
if is_huiwen(a[i:j+1]):
max_length = len(a[i:j+1])
sub_longest = a[i:j+1]
return max_length, sub_longest
a= "abccbaab1233245"
max_length, sub_longest = get_longest_huiwen_sub(a)
print(max_length, sub_longest)
Original: https://www.cnblogs.com/LanTianYou/p/16420036.html
Author: 天外归云
Title: Python找字符串中的最长回文子串
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/15737/
转载文章受原作者版权保护。转载请注明原作者出处!