← 返回 walmartlabs 的题目列表Insert Spaces to Highlight the Longest Palindromic Prefix/Suffix Match
类型:online_judge
Problem
Given a string s. If s can be written as:
s = L + M + reverse(L)
where L can be empty, M is the remaining middle part, and reverse(L) is L reversed.
Output the string by inserting single spaces between characters in L and reverse(L) (character-by-character), while keeping the middle part M unchanged. If adjacent parts exist, separate them by a single space.
Example:
Input: abcxyba
L = "ab", M = "cxy", reverse(L) = "ba"
Output: a b cxy b a
If multiple decompositions are possible, choose the one with the longest L.
I/O
Input: a single string s
Output: the transformed string
Constraints
1 <= len(s) <= 2 * 10^5
s contains visible ASCII characters (no spaces)
Expected complexity: O(n) or O(n log n)
Examples
s = "abcxyba" → "a b cxy b a"
s = "abba" → "a b b a" (M is empty)
s = "abcd" → "abcd" (L is empty)
Example
Input
abcxyba
Output
a b cxy b a