Palindrome with K Deletions
Updated
Problem statement
Given a string s and an integer k, return true if s can become a palindrome after deleting at most k characters. Otherwise, return false.
A palindrome reads the same from left to right and right to left. Deleting characters keeps the rest in their original order. Deleting nothing is allowed.
Function
isValidPalindrome(s: String, k: int) → booleanExamples
Example 1
s = "abxcbya"k = 2return = trueDelete "x" and "y" to get "abcba", which is a palindrome. That uses 2 deletions.
Example 2
s = "abcd"k = 2return = falseAll four letters are different, so a palindrome can keep at most one of them. That takes 3 deletions, more than k = 2.
Example 3
s = "level"k = 0return = true"level" is already a palindrome, so no deletion is needed.
Example 4
s = "cabbad"k = 1return = falseThe ends "c" and "d" differ, so one deletion must remove one of them. Deleting "c" leaves "abbad" and deleting "d" leaves "cabba". Neither is a palindrome.
Constraints
1 <= s.length <= 1000scontains only lowercase English letters.0 <= k <= s.length
More WhatNot Phone Screen questions
- Adjacent Pair and Run RemovalPHONE SCREEN · Seen Nov 2026
- Merge Sorted Squares of Two ArraysPHONE SCREEN · Seen Nov 2026
- User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Knight Dialer SequencesPHONE SCREEN · Seen Oct 2026
- Reachable Exits in an Obstacle GridPHONE SCREEN · Seen Oct 2026
- Palindrome with One DeletionPHONE SCREEN · Seen Oct 2026
- Search a 2D MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Attraction Route Without Reusing TrailsPHONE SCREEN · Seen Sep 2026
- Earliest Car for Each PassengerPHONE SCREEN · Seen Sep 2026
- Minimum Stores for a Shopping ListPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Concatenated WordsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Balanced Parentheses with Sequential Left DeletionsGoogle · PHONE SCREENGoogle interview questions
- Word Break IIWex · PHONE SCREEN
- Word Break IIInMobi · PHONE SCREENInMobi interview questions
- Word Break IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Word Break II: All SentencesMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Longest Valid ParenthesesApple · PHONE SCREENApple interview questions
- Longest Properly Closed Bracket RunAdobe · PHONE SCREENAdobe interview questions
- Indexed Proximity SearchStripe · PHONE SCREENStripe interview questions
- Longest Palindromic SubstringSiemens · PHONE SCREEN