Problem · String
Minimum Deletions for a Non-Palindrome
Learn this problemProblem statement
You are given a non-empty lowercase string text. In one operation, you may delete any one character while preserving the relative order of all remaining characters.
Return the minimum number of deletions needed to make the remaining string a non-palindrome. If text is already a non-palindrome, return 0. If no sequence of deletions can produce a non-palindrome, return -1.
Strings of length zero or one are considered palindromes.
Function
minimumDeletionsForNonPalindrome(text: String) → intExamples
Example 1
text = "abca"return = 0abca is already different from its reverse, so no deletion is needed.
Example 2
text = "abba"return = 1The original string is a palindrome. Deleting its first character produces bba, which is not a palindrome.
Example 3
text = "aaaa"return = -1Every string obtainable by deletion contains only a characters, so every possible remainder is a palindrome.
Constraints
1 <= text.length <= 100000textcontains only lowercase English letters.