-
Notifications
You must be signed in to change notification settings - Fork 0
/
046.py
36 lines (25 loc) · 963 Bytes
/
046.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
"""
Problem:
Given a string, find the longest palindromic contiguous substring. If there are more
than one with the maximum length, return any one.
For example, the longest palindromic substring of "aabcdcb" is "bcdcb". The longest
palindromic substring of "bananas" is "anana".
"""
def is_palindrome(string: str) -> bool:
# helper function to check if a string is a palindrome
return string == string[::-1]
def get_longest_palindrome_substring(string: str) -> str:
if is_palindrome(string):
return string
# generating the longest palindromic substring
string1 = get_longest_palindrome_substring(string[1:])
string2 = get_longest_palindrome_substring(string[:-1])
return max(string1, string2, key=lambda s: len(s))
if __name__ == "__main__":
print(get_longest_palindrome_substring("aabcdcb"))
print(get_longest_palindrome_substring("bananas"))
"""
SPECS:
TIME COMPLEXITY: O(n ^ 2)
SPACE COMPLEXITY: O(n)
"""