Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use python to find the maximum palindrome in a string

Shulou Source: shulou.com Published: 2022-06-01 11:32:36 09月12日 Update

This article mainly explains "how to use python to find the maximum palindrome in a string". The explanation in the article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use python to find the maximum palindrome in a string".

The requirements of the topic are as follows:

Find the largest palindrome in a string, such as the string "sddcbabcdtre", where the maximum palindrome is "dcbabcd"

Topic analysis

A palindrome means that a string looks the same from left to right and from right to left.

The title states that the string contains a palindrome, not that the whole string is a palindrome.

The length of palindromes can be even or odd

A string of length 1 is not a palindrome

Train of thought analysis

Traverse the string

When each traversal reaches one character, take it as the center to verify whether it constitutes a palindrome to the left and right.

Variables are required to record the starting position and length of palindromes

Sample code

# coding=utf-8

Str_value = "sddcbabcdtre"

# str_value = "aaa"

I = 0

Start_index = 0 # record the starting position of palindromes

Plalindrome_len = 0

While i

< len(str_value)-1: tmp_len = 0 left,right = 0,0 #假设回文长度是偶数 if str_value[i] == str_value[i+1]: # 找到下一个要比较的位置 left,right = i-1,i+2 tmp_len = 2 #注意循环的条件 while left >

= 0 and right

< len(str_value): if str_value[left] == str_value[right]: left -= 1 #向前 right += 1 #向后 tmp_len += 2 #长度要加2 else: break if tmp_len >

Plalindrome_len:

Plalindrome_len = tmp_len

Start_index = left + 1 # add 1 here

# suppose the palindrome length is odd

If I > 0 and str_ value [I-1] = str_value [iTunes 1]:

Left,right = iMel 2

Tmp_len = 3

While left > = 0 and right

< len(str_value): if str_value[left] == str_value[right]: left -= 1 right += 1 tmp_len += 2 else: break if tmp_len >

Plalindrome_len:

Plalindrome_len = tmp_len

Start_index = left + 1

I + = 1

If plalindrome_len = = 0:

Print'no plalindrome'

Else:

Print str_ value[start _ index:

Start_index+plalindrome_len]

Analysis of key points

The key to the program is to center on a character and traverse the left and right sides to determine whether it constitutes a palindrome, which is much like folding a piece of paper in half.

For while loops, since you are traversing a string, you must check the boundaries at both ends to cross the bounds, which is indispensable

There may be multiple palindromes in the program, so if you want to find the largest palindrome, you must use tmp_len to compare it with the largest palindrome that has been found.

Thank you for your reading, the above is the content of "how to use python to find the maximum palindrome in a string". After the study of this article, I believe you have a deeper understanding of how to use python to find the maximum palindrome in a string. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Palindromes characters strings maximum length location topic Learning even key content Odd idea Program Analysis Loop start validation both ends Code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Apple Shulou Information Docker Shulou Tech Info