-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestPalindrome.py
37 lines (34 loc) · 1.04 KB
/
LongestPalindrome.py
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
"""
LeetCode 5: Longest Palindromic Substring
example:
input: "babad"
Ouput: can be "bab" or "aba"
Time Complexity: O(n*2)
"""
class LongestPalindrome:
def findLongestPalindrome(self, s:str) -> str:
if len(s) == 1:
return s
longest = 0
curr_length = 0
sub_string = ""
palindrome = ""
for i in range(len(s)):
count = 0
for j in range(i+1,len(s)):
if count == 0:
sub_string = s[i] + s[j]
count += 1
else:
sub_string = sub_string + s[j]
count += 1
if sub_string == sub_string[::-1]:
curr_length = len(sub_string)
if curr_length > longest:
longest = curr_length
palindrome = sub_string
if palindrome == "":
palindrome = s[i]
return palindrome
subString = LongestPalindrome()
print(subString.findLongestPalindrome("ac"))