Minimum Deletions for a Non-Palindrome
Updated
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) → 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.
More Deloitte Online Assessment questions
- Seventh Nearest PalindromeOA · Seen May 2026
- Minimum-Cost Solar Rooftop AllocationOA · Seen Jul 2025
- Pair Elements with Even SumsOA · Seen Jul 2025
- Count Divisible PermutationsOA · Seen Jan 2025
- Largest Element in an ArrayOA · Seen Nov 2024
- Install Carbon FiltersOA · Seen Oct 2023
- Longest Substring with Even OccurrencesOA · Seen Oct 2023
- Maximum Sum RectangleOA · Seen Apr 2022
- Annual Bank Balance with Monthly Card FeesOA · Seen Jan 2022
- Count Battleships by SizeOA · Seen Jan 2022
Similar questions at other companies
- Rearrange String by Alternating EndsHudson River Trading · OAHudson River Trading interview questions
- Alternate String EndsCapital One · OACapital One interview questions
- Run-Length String CompressionSalesforce · OASalesforce interview questions
- Minimum Changes for Palindromic BlocksCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
- Count Binary SubstringsMicrosoft · OAMicrosoft interview questions
- Check a Repeated String as a SubsequenceQuince · OAQuince interview questions
- Reverse String In PlaceThe Walt Disney Company · OA · PHONE SCREENThe Walt Disney Company interview questions
- Lost MessagesSuperhuman · OASuperhuman interview questions
- Reverse a StringFlexTrade · OAFlexTrade interview questions
- Missing WordsVanguard · OAVanguard interview questions