-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc-13.34.py
29 lines (24 loc) · 809 Bytes
/
c-13.34.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
"""
Describe an efficient algorithm to find the longest palindrome that is a
suffix of a string T of length n. Recall that a palindrome is a string that is
equal to its reversal. What is the running time of your method?
"""
def longest_palindrome_in_suffix_of(T):
left, right = 0, len(T)-1
max_palindrome = 0
while left < right:
if T[left] == T[right]:
left += 1
right -= 1
else:
left += 1
right = len(T)-1
max_palindrome = left
return T[max_palindrome:]
if __name__ == "__main__":
T = "banana"
assert longest_palindrome_in_suffix_of(T) == "anana"
T = "anavolimilovana"
assert longest_palindrome_in_suffix_of(T) == "anavolimilovana"
T = ""
assert longest_palindrome_in_suffix_of(T) == ""