-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython task
More file actions
45 lines (31 loc) · 1.02 KB
/
python task
File metadata and controls
45 lines (31 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def printSubStr(str, low, high):
for i in range(low, high + 1):
print(str[i], end = "")
# This function prints the longest palindrome subString It also returns the length of the longest palindrome
def longestPalSubstr(str):
# Get length of input String
n = len(str)
# All subStrings of length 1 are palindromes
maxLength = 1
start = 0
# Nested loop to mark start and end index
for i in range(n):
for j in range(i, n):
flag = 1
# Check palindrome
for k in range(0, ((j - i) // 2) + 1):
if (str[i + k] != str[j - k]):
flag = 0
# Palindrome
if (flag != 0 and (j - i + 1) > maxLength):
start = i
maxLength = j - i + 1
print("Longest palindrome subString is: ", end = "")
printSubStr(str, start, start + maxLength - 1)
# Return length of LPS
return maxLength
# Driver Code
if __name__ == '__main__':
str = "forgeeksskeegfor"
print("\nLength is: ", longestPalSubstr(str))
# im not gonna lie, this is a google solution, but at least i sarched and found this solution ^-^