243. Append Minimum Characters At End To Make Palindrome
Asked in
Append Minimum Characters At End To Make Palindrome
Given a string s, find the minimum number of characters that must be appended to the end of s so that the final string becomes a palindrome.
You only need to return the minimum count of appended characters, not the final palindrome string.

Class

PalindromeAppender

Method

Minimum Characters To Append

int minimumCharactersToAppend(String s)
  • Returns the minimum number of characters that must be appended at the end of s.
  • The appended characters can be chosen freely from lowercase English letters.
  • The final string must read the same from left to right and right to left.
  • If s is already a palindrome, return 0.

Constraints

  • 1 ≤ s.length() ≤ 100,000
  • s contains only lowercase English letters.

Examples

Example 1

minimumCharactersToAppend(s = "race")
Output: 3
Explanation: Append "car" to get "racecar", which is a palindrome.

Example 2

minimumCharactersToAppend(s = "abab")
Output: 1
Explanation: Append "a" to get "ababa", which is a palindrome.

Example 3

minimumCharactersToAppend(s = "madam")
Output: 0
Explanation: The string is already a palindrome, so no character needs to be appended.

Example 4

minimumCharactersToAppend(s = "abcdc")
Output: 2
Explanation: Append "ba" to get "abcdcba", which is a palindrome.


Please use Laptop/Desktop or any other large screen to add/edit code.