Given a string s0 find the lexicographically smallest palindrome of the same length that is lexicographically greater than or equal tos0
.
Strings may contain only lowercase English letters.
Example
- For
s0 = "cbaba"
, the output should be
smallestPalindrome(s0) = "cbabc";
- For
s0 = "abcbc"
, the output should be
smallestPalindrome(s0) = "abdba"
.
Input/Output
- [time limit] 4000ms (js)
- [input] string s0
A string containing only lowercase English letters.
Guaranteed constraints:
4 ≤ s0.length ≤ 5 · 10^5.
[output] string
The required palindrome.
Help me define steps in solving this algorithm I can’t recognize the pattern, so I am not sure what to do. Any clues will be helpful, I understand what is lexicographically smallest palindrome. What I don’t understand is the logic behind generating new palindrome, when can I introduce a new letter that hasn’t been part of input?