String Matching Pattern matching
579. Shortest Palindrome
You are given a string s. You may add letters to the front of s, and nowhere else. Return the shortest
palindrome you can make this way.
A palindrome reads the same forwards and backwards, such as "noon" or "refer". The empty string is
a palindrome too. Answers are shown in double quotes, so an empty answer is "".
Example 1
Input: s = "abacus"
"sucabacus"Explanation: The longest start of abacus that is already a palindrome is aba. The letters after it, cus, are
added to the front in reverse order, as suc.
Example 2
Input: s = ""
""Explanation: The empty string is already a palindrome, so nothing is added.
Example 3
Input: s = "abcd"
"dcbabcd"Explanation: Only the first letter is a palindrome on its own, so the other three letters go to the front, reversed.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
0 ≤ s.length ≤ 105sholds only lowercase English letters.
Clarifying questions
What you would ask an interviewer before writing any code.