263. Shortest Palindrome (Prepend Count)

HardStringString

Given a string s, you can convert it to a palindrome by adding characters in front of it. Return the minimum number of characters that must be prepended to make s a palindrome.

Input: A quoted string s.

Output: Integer — minimum characters to prepend.

Examples

Example 1
Input: "aacecaaa"
Output: 1
Explanation: Prepend 'a' -> 'aaacecaaa'.
Example 2
Input: "abcd"
Output: 3
Explanation: Prepend 'dcb'.
Example 3
Input: ""
Output: 0
Explanation: Already a palindrome.

Constraints

Asked by

AccentureBloombergGoogleMicrosoftAmazonMeta
Solve this problem in the editor →