FastPrepMinimum Deletions for a Non-Palindrome
Problem · String

Minimum Deletions for a Non-Palindrome

Learn this problem
EasyDeloitte logoDeloitteNEW GRADOA

Problem 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) → int

Examples

Example 1

text = "abca"return = 0

abca is already different from its reverse, so no deletion is needed.

Example 2

text = "abba"return = 1

The original string is a palindrome. Deleting its first character produces bba, which is not a palindrome.

Example 3

text = "aaaa"return = -1

Every string obtainable by deletion contains only a characters, so every possible remainder is a palindrome.

Constraints

  • 1 <= text.length <= 100000
  • text contains only lowercase English letters.

More Deloitte problems

drafts saved locally
public int minimumDeletionsForNonPalindrome(String text) {
    // Write your solution here.
}
text"abca"
expected0
checking account