Data Structures & AlgorithmsVery hardProduct Company SDE · ICPC regional

The string s = "abcbdab" needs characters deleted to become a palindrome. What is the minimum number of deletions? Use n minus the longest palindromic subsequence, computed by running the DP.

The string s = "abcbdab" needs characters deleted to become a palindrome. What is the minimum number of deletions? Use n minus the longest palindromic subsequence, computed by running the DP.
s="abcbdab"
n=len(s)
dp=[[0]*n for _ in range(n)]
for i in range(n-1,-1,-1):
    dp[i][i]=1
    for j in range(i+1,n):
        dp[i][j]=dp[i+1][j-1]+2 if s[i]==s[j] else max(dp[i+1][j],dp[i][j-1])
print(n-dp[0][n-1])
What is printed?
  1. A. 5
  2. B. 4
  3. C. 3
  4. D. 2

Correct answer: D. 2

Deleting to a palindrome keeps a longest palindromic subsequence, so the deletions are n minus its length. Using the longest palindromic substring deletes too many characters, counting distinct letters is unrelated, and reporting the kept length is a misreading.

More Coding Interview Problems questions

Exams that ask this topic