Prepin
Log in

Determine whether a string is a K-Palindrome.

Approach / Explanation

This question assesses two key areas of your programming skills: your grasp of string concepts and palindromes. your capability to implement efficient algorithms for string manipulation. The term 'K-Palindrome' describes a string that can be turned into a palindrome by removing no more than 'K' characters. A palindrome is defined as a string that reads identically forwards and backwards. In the case of a K-palindrome, you have the option to remove certain characters to form a palindrome. Your task is to create a method or function that determines whether a given string qualifies as a K-palindrome. To devise an efficient solution: Consider using logical and dynamic programming strategies to tackle this problem instead of a brute force method. Your algorithm should first verify if the string is a palindrome. If the string is not a palindrome, it should then ascertain whether it can become one by removing 'K' or fewer characters. Keep in mind that the sequence of character removal is irrelevant. The primary concern is whether the string can be transformed into a palindrome with 'K' or fewer deletions.

Suggested Answer

A practical method to address this issue involves utilizing dynamic programming. Begin by determining the length of the longest palindromic subsequence within the string. Next, subtract this length from the total length of the original string. If the result is less than or equal to 'K', then the string qualifies as a K-Palindrome; otherwise, it does not. Below is a Python code example to demonstrate this: def KPalindrome ( s , k ) : n = len ( s ) dp = [ [ 0 for _ in range ( n + 1 ) ] for _ in range ( n + 1 ) ] for i in range ( n , - 1 , - 1 ) : for j in range ( n , i , - 1 ) : if s [ i ] == s [ j ] : dp [ i ] [ j ] = dp [ i + 1 ] [ j - 1 ] + 2 else : dp [ i ] [ j ] = max ( dp [ i ] [ j - 1 ] , dp [ i + 1 ] [ j ] ) return "YES" if ( n - dp [ 0 ] [ n - 1 ] ) <= k else "NO"

Alternative Answer

One alternative method to tackle this question is by utilizing the concept of "edit distance." The edit distance between two strings refers to the smallest number of operations needed to transform one string into another, with the operations being insertion, deletion, or replacement. In this scenario, we will focus solely on deletions. Therefore, if the least number of deletions necessary to turn a string into a palindrome (or the minimum insertions required for the same) is less than or equal to 'K', then the string qualifies as a K-Palindrome. Below is a Python code example to demonstrate this: def minDeletions ( s ) : n = len ( s ) dp = [ [ 0 for _ in range ( n ) ] for _ in range ( n ) ] for gap in range ( 1 , n ) : l = 0 for h in range ( gap , n ) : if s [ l ] == s [ h ] : dp [ l ] [ h ] = dp [ l + 1 ] [ h - 1 ] else : dp [ l ] [ h ] = min ( dp [ l ] [ h - 1 ] , dp [ l + 1 ] [ h ] ) + 1 l += 1 return dp [ 0 ] [ n - 1 ] def isKPal ( s , k ) : l = minDeletions ( s ) return True if l <= k else False In the provided code snippet, minDeletions(s) calculates the minimum number of deletions required to transform the string into a palindrome, while the function isKPal(s, k) returns True if the string is classified as a K-palindrome and False otherwise.

Question Details

Difficulty & Category

Medium

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.