2020-10-06 08:31:15 +00:00
|
|
|
def palindromic_string(input_string: str) -> str:
|
2019-01-02 11:17:56 +00:00
|
|
|
"""
|
2020-02-05 11:27:43 +00:00
|
|
|
>>> palindromic_string('abbbaba')
|
|
|
|
'abbba'
|
|
|
|
>>> palindromic_string('ababa')
|
|
|
|
'ababa'
|
|
|
|
|
2024-04-22 19:51:47 +00:00
|
|
|
Manacher's algorithm which finds Longest palindromic Substring in linear time.
|
2019-01-02 11:17:56 +00:00
|
|
|
|
2020-01-18 12:24:33 +00:00
|
|
|
1. first this convert input_string("xyx") into new_string("x|y|x") where odd
|
|
|
|
positions are actual input characters.
|
2024-04-19 19:30:22 +00:00
|
|
|
2. for each character in new_string it find corresponding length and
|
|
|
|
store the length and left,right to store previously calculated info.
|
|
|
|
(please look the explanation for details)
|
2020-05-22 06:10:11 +00:00
|
|
|
|
2020-02-05 11:27:43 +00:00
|
|
|
3. return corresponding output_string by removing all "|"
|
2019-01-02 11:17:56 +00:00
|
|
|
"""
|
|
|
|
max_length = 0
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2019-01-02 11:17:56 +00:00
|
|
|
# if input_string is "aba" than new_input_string become "a|b|a"
|
|
|
|
new_input_string = ""
|
|
|
|
output_string = ""
|
|
|
|
|
|
|
|
# append each character + "|" in new_string for range(0, length-1)
|
2019-10-05 05:14:13 +00:00
|
|
|
for i in input_string[: len(input_string) - 1]:
|
2019-01-02 11:17:56 +00:00
|
|
|
new_input_string += i + "|"
|
2019-10-05 05:14:13 +00:00
|
|
|
# append last character
|
2019-01-02 11:17:56 +00:00
|
|
|
new_input_string += input_string[-1]
|
|
|
|
|
2020-05-22 06:10:11 +00:00
|
|
|
# we will store the starting and ending of previous furthest ending palindromic
|
|
|
|
# substring
|
2024-04-19 19:30:22 +00:00
|
|
|
left, right = 0, 0
|
2020-02-05 11:27:43 +00:00
|
|
|
|
|
|
|
# length[i] shows the length of palindromic substring with center i
|
|
|
|
length = [1 for i in range(len(new_input_string))]
|
|
|
|
|
2019-01-02 11:17:56 +00:00
|
|
|
# for each character in new_string find corresponding palindromic string
|
2021-08-25 11:35:36 +00:00
|
|
|
start = 0
|
|
|
|
for j in range(len(new_input_string)):
|
2024-04-19 19:30:22 +00:00
|
|
|
k = 1 if j > right else min(length[left + right - j] // 2, right - j + 1)
|
2020-02-05 11:27:43 +00:00
|
|
|
while (
|
2021-08-25 11:35:36 +00:00
|
|
|
j - k >= 0
|
|
|
|
and j + k < len(new_input_string)
|
|
|
|
and new_input_string[k + j] == new_input_string[j - k]
|
2020-02-05 11:27:43 +00:00
|
|
|
):
|
|
|
|
k += 1
|
|
|
|
|
2021-08-25 11:35:36 +00:00
|
|
|
length[j] = 2 * k - 1
|
2019-01-02 11:17:56 +00:00
|
|
|
|
2024-04-19 19:30:22 +00:00
|
|
|
# does this string is ending after the previously explored end (that is right) ?
|
|
|
|
# if yes the update the new right to the last index of this
|
|
|
|
if j + k - 1 > right:
|
|
|
|
left = j - k + 1
|
|
|
|
right = j + k - 1
|
2019-01-02 11:17:56 +00:00
|
|
|
|
|
|
|
# update max_length and start position
|
2021-08-25 11:35:36 +00:00
|
|
|
if max_length < length[j]:
|
|
|
|
max_length = length[j]
|
|
|
|
start = j
|
2019-10-05 05:14:13 +00:00
|
|
|
|
|
|
|
# create that string
|
2020-02-05 11:27:43 +00:00
|
|
|
s = new_input_string[start - max_length // 2 : start + max_length // 2 + 1]
|
|
|
|
for i in s:
|
2019-01-02 11:17:56 +00:00
|
|
|
if i != "|":
|
|
|
|
output_string += i
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2019-01-02 11:17:56 +00:00
|
|
|
return output_string
|
|
|
|
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
2020-02-05 11:27:43 +00:00
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|
|
|
|
|
|
|
|
"""
|
|
|
|
...a0...a1...a2.....a3......a4...a5...a6....
|
|
|
|
|
2020-05-22 06:10:11 +00:00
|
|
|
consider the string for which we are calculating the longest palindromic substring is
|
|
|
|
shown above where ... are some characters in between and right now we are calculating
|
|
|
|
the length of palindromic substring with center at a5 with following conditions :
|
2024-04-19 19:30:22 +00:00
|
|
|
i) we have stored the length of palindromic substring which has center at a3
|
|
|
|
(starts at left ends at right) and it is the furthest ending till now,
|
|
|
|
and it has ending after a6
|
2020-02-05 11:27:43 +00:00
|
|
|
ii) a2 and a4 are equally distant from a3 so char(a2) == char(a4)
|
|
|
|
iii) a0 and a6 are equally distant from a3 so char(a0) == char(a6)
|
2020-05-22 06:10:11 +00:00
|
|
|
iv) a1 is corresponding equal character of a5 in palindrome with center a3 (remember
|
|
|
|
that in below derivation of a4==a6)
|
2020-02-05 11:27:43 +00:00
|
|
|
|
2020-05-22 06:10:11 +00:00
|
|
|
now for a5 we will calculate the length of palindromic substring with center as a5 but
|
|
|
|
can we use previously calculated information in some way?
|
|
|
|
Yes, look the above string we know that a5 is inside the palindrome with center a3 and
|
2022-05-01 10:44:23 +00:00
|
|
|
previously we have calculated that
|
2020-02-05 11:27:43 +00:00
|
|
|
a0==a2 (palindrome of center a1)
|
|
|
|
a2==a4 (palindrome of center a3)
|
|
|
|
a0==a6 (palindrome of center a3)
|
|
|
|
so a4==a6
|
|
|
|
|
2020-05-22 06:10:11 +00:00
|
|
|
so we can say that palindrome at center a5 is at least as long as palindrome at center
|
|
|
|
a1 but this only holds if a0 and a6 are inside the limits of palindrome centered at a3
|
|
|
|
so finally ..
|
2020-02-05 11:27:43 +00:00
|
|
|
|
2024-04-19 19:30:22 +00:00
|
|
|
len_of_palindrome__at(a5) = min(len_of_palindrome_at(a1), right-a5)
|
|
|
|
where a3 lies from left to right and we have to keep updating that
|
2020-02-05 11:27:43 +00:00
|
|
|
|
2024-04-19 19:30:22 +00:00
|
|
|
and if the a5 lies outside of left,right boundary we calculate length of palindrome with
|
|
|
|
bruteforce and update left,right.
|
2020-02-05 11:27:43 +00:00
|
|
|
|
|
|
|
it gives the linear time complexity just like z-function
|
|
|
|
"""
|