-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-pass.py
More file actions
37 lines (27 loc) · 909 Bytes
/
Copy pathpython-pass.py
File metadata and controls
37 lines (27 loc) · 909 Bytes
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
class Solution:
def printSubStr(str, low, high):
for i in range(low, high + 1):
print(str[i], end = "")
def longest_palindromic(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 : ", end = "")
printSubStr(str, start, start + maxLength - 1)
return maxLength