Ctrl K

Palindrome by Adding Letters in Front

Hard
You are given a word word. You may add letters only to its beginning. Turn it into a palindrome, a string that reads the same left to right and right to left, adding as few letters as possible, and return the result. For an empty word the answer is an empty string.
Example 1
word="level" → 'level'

The word is already a palindrome, so nothing needs to be added.

Example 2
word="noonday" → 'yadnoonday'

The longest palindromic start is noon. The rest, day, is reversed and put in front: yadnoonday.

Example 3
word="kayaks" → 'skayaks'

The start kayak is a palindrome, so one s in front is enough.

Constraints
  • 0 ≤ len(word) ≤ 5 · 10⁵
  • word consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsStart That Matches the End HardSuffixes Echoing the Start HardPeel Off a Repeated Start HardWord Near an Anchor HardChop and Append Hard
loading Python…
Input
word="level"
Expected
'level'

Run checks the first 3 tests in your browser. Submit checks all 160 tests on the server, hidden ones included.