SoFunction
Updated on 2024-11-17

Python Real Example of Longest Echo Substring Periodic String Details

I. The longest iambic pentameter string

Description of the problem🪐

You are already familiar with the fact that strings of the type AABCC, AABBCC arepalindrome

That is to say, exclude all kinds of characters in the string, the letters are not case-sensitive, and complete the selection of the longest substring of the return text can be. If there are several strings of the same length, you need to use the leftmost substring, the output of the output as is

Sample Input:

“Confuciuss say:Madam,I'm Adam”

Sample output:

“Mandam,I'm Adam”

Problem Analysis🪐

The first step should be to remove the special characters within the original string to get a string containing only letters

The second step is to perform echo substring picking in a purely alphabetic string

Picking out the specified palindrome string also requires an as-is output

So you should record the coordinates of the first and last characters of the substring in the original string.

It is possible to define an array as long as a purely alphabetic substring. When filtering for whitespace the index of the character in the original string is recorded.

Code implementation🪐

The usual rules apply first with the results of the run:

There are two implementations used here, one in the C language style and one in Python The main difference between the two is that Python may have some libraries to facilitate the judgment.

# C style implementations
import sys
mystr=().strip()
charr=""
charri=[]
mymax=-1
x=0
y=0
flag=True

j=-1
for i in mystr:
    j+=1
    if ord(i)<65 or ord(i)>122:
        continue
    else:
        charr+=i
        (j)
charr=()

# print(charr,charri)
i=0
while i<len(charr):
    j=i
    while j<len(charr):
        k=i
        while k<=j:
            if charr[k]!=charr[i+j-k]:
                flag=False
                break
            k+=1
        if flag:
            if mymax<j-i+1:
                mymax=j-i+1
                x=i
                y=j
        flag=True
        j+=1
    i+=1 

print("First realization:")
print(x,y)
print(mystr[charri[x]:charri[y]+1:])

# python style implementation

import sys
mstr=().strip()

tstr=""
snum=[]
smax=0
x=0
y=0
j=0
for i in mstr:
    if ord(i)>=65 and ord(i)<=122:
        tstr+=i
        (j)
    j+=1

tstr=()

for i in  range(len(tstr)):
    for j in range(i,len(tstr)+1):
        if tstr[i:j]==tstr[i:j][::-1] and len(tstr[i:j])>smax:
            smax=len(tstr[i:j])
            x=i
            y=j
print("Second realization:")
print(x,y)
print(mstr[snum[x]:snum[y-1]+1])

II. Periodic strings

Description of the problem🪐

If a string can be obtained by repeating a substring of length k for more than one cycle, then we say that the string is a periodic string with k cycles

For example: qweqweqwe (with a period of 3), ababababab (can have a period of 2,4)

Our task is to input a string and then find the minimum number of cycles of that string

Sample Input:

HoHoHo

Sample output:

2

Problem Analysis🪐

First, read the string, then select a cycle, determine whether the next cycle substring and the previous cycle substring correspond to the same position in the string.

If there is a position that is not the same, it is determined that it is not a periodic string, because we are looking for the smallest period, we can start from 1. If we are looking for the largest number of periods, we can start from the length of the main string.

Code implementation🪐

The usual rules apply first with the results of the run:

import sys 
mmax=0
mystr=().strip()
for i in range(1,len(mystr)):
    if len(mystr)%i==0:
        for j in range(0,len(mystr)//i-1):
            if mystr[j*i:j*i+i]!=mystr[(j+1)*i:(j+1)*i+i]:
                # print(mystr[j*i:j*i+i],"--",len(mystr[(j+1)*i:(j+1)*i+i]))
                break
        else:
            mmax=i
    if mmax!=0:
        break
    
print(mmax)

ᴴᴬᵛᴱ ᴬ ᴳᴼᴼᴰ ᵀᴵᴹᴱ !

To this point, this Python questions about the case of the longest palindromic substring cycle string detailed article is introduced to this, more related Python longest palindromic substring content, please search for my previous posts or continue to browse the following related articles I hope you will support me more in the future!