Longest Chunked Palindrome Decomposition Problem
Description
LeetCode Problem 1147.
You are given a string text. You should split it to k substrings (subtext_1, subtext_2, ..., subtext_k) such that:
subtext_iis a non-empty string.- The concatenation of all the substrings is equal to
text(i.e.,subtext_1 + subtext_2 + ... + subtext_k == text). subtext_i == subtext_(k - i + 1)for all valid values ofi(i.e.,1 <= i <= k).
Return the largest possible value of k.
Example 1:
Input: text = "ghiabcdefhelloadamhelloabcdefghi"
Output: 7
Explanation: We can split the string on "(ghi)(abcdef)(hello)(adam)(hello)(abcdef)(ghi)".
Example 2:
Input: text = "merchant"
Output: 1
Explanation: We can split the string on "(merchant)".
Example 3:
Input: text = "antaprezatepzapreanta"
Output: 11
Explanation: We can split the string on "(a)(nt)(a)(pre)(za)(tep)(za)(pre)(a)(nt)(a)".
Constraints:
1 <= text.length <= 1000textconsists only of lowercase English characters.
Solution
Approach: Greedy, Cut the Shortest Matching Ends
Work from both ends of the string at once. Find the shortest non-empty prefix that equals the suffix of the same length, with the two not overlapping:
- If there is one, cut it off at both ends as the outermost pair of chunks, add
2, and repeat on the middle part. - If there is none, the remaining middle part can only be one single chunk: add
1and stop. If nothing is left in the middle, add nothing.
Why is cutting the shortest pair A ... A always safe? Take a best split whose outer chunks are a longer pair B ... B. The text starts with both A and B, so B starts with A; it also ends with both, so B ends with A. When B is at least twice as long as A, B = A + X + A, and we can split each copy of B further into A, X, A (for an empty X, just A, A). The left side becomes A, X, A, ... and the right side ..., A, X, A, which still mirror each other, so we get even more chunks. (If B were shorter than twice A, the two As would overlap inside B, and B would then have an even shorter matching prefix and suffix, contradicting that A is the shortest.) So an optimal answer can always start with A ... A.
For example, "antaprezatepzapreanta": cut a|a, then nt|nt, then a|a, then pre|pre, then za|za, and tep is left in the middle: 2 + 2 + 2 + 2 + 2 + 1 = 11.
Complexity: O(n2) time: a cut of length k tries k prefix lengths with comparisons of up to k characters, the cut lengths add up to at most n / 2, and the final failed search over a middle of length m costs O(m2). The substrings compared take O(n) extra space (O(1) when the languages compare in place).
class Solution:
def longestDecomposition(self, text: str) -> int:
chunks = 0
i, j = 0, len(text) # the part still to split is text[i:j]
while i < j:
# Look for the shortest prefix of text[i:j] that is also its suffix, without overlap.
k = 1
while 2 * k <= j - i and text[i:i + k] != text[j - k:j]:
k += 1
if 2 * k > j - i:
return chunks + 1 # no such pair: the whole middle part is one chunk
chunks += 2
i += k
j -= k
return chunks