-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path46_Amazon_Longest_Palindromic_Subsequence.py
executable file
·68 lines (46 loc) · 1.81 KB
/
46_Amazon_Longest_Palindromic_Subsequence.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
This problem was asked by Amazon.
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 check_palindrome(s):
if len(s)==1:
return True
for i in range(len(s)//2):
if s[i] != s[-1 - i]:
return False
return True
def find_longest_palindromic_substr(s):
longest_substr = ""
def recursive_helper(s, substr="", iter=0):
nonlocal longest_substr # to access variable from outer scope
if iter >= len(s):
return
if check_palindrome(substr + s[iter]):
if len(substr + s[iter]) > len(longest_substr):
longest_substr = substr + s[iter]
recursive_helper(s, substr+s[iter], iter+1)
recursive_helper(s, substr + s[iter], iter+1)
recursive_helper(s, s[iter], iter+1)
recursive_helper(s)
return longest_substr
# -----------------------------------------
# A much cleaner and simpler approach
def check_palindrome2(s):
return s == s[::-1]
def find_longest_palindromic_substr2(s):
if check_palindrome2(s):
return s
s1 = find_longest_palindromic_substr2(s[1:])
s2 = find_longest_palindromic_substr2(s[:-1])
return s1 if len(s1) > len(s2) else s2
if __name__ == "__main__":
# print(find_longest_palindromic_substr('aabcdcb'))
# print(find_longest_palindromic_substr('bananas'))
# print(find_longest_palindromic_substr('johanna111222111'))
# -------------------------
print(find_longest_palindromic_substr2('aabcdcb'))
print(find_longest_palindromic_substr2('bananas'))
print(find_longest_palindromic_substr2('johanna111222111'))