-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromic Substring.py
More file actions
38 lines (34 loc) · 1.02 KB
/
Palindromic Substring.py
File metadata and controls
38 lines (34 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
"""
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example 2:
Input: "cbbd"
Output: "bb"
"""
def longestPalindrome(s):
"""
:type s: str
:rtype: str
"""
length = len(s)
longestString = ""
# Use i in range as opposed to foreach to keep track of index at
for i in range(0,length - 1):
if s[i] == s[i+1]:
currString = s[i] + s[i+1]
# Prevents out of range
if i > 0:
for j in range(1,i+1):
# Prevents out of range
if (i+1)+j < len(s):
if s[i-j] == s[(i+1)+j]:
currString = s[i-j] + currString + s[(i+1)+j]
else:
break
if len(currString) > len(longestString):
longestString = currString
return longestString
print(longestPalindrome("cabbacd"))