00:00

Problem Statement

Input/Output Examples

Visual Explanation

Every cell depends only on shorter substrings: the cell below-left when the ends match, or the cell below or to the left when they do not. Filling the table from short substrings to long ones guarantees those values are ready.

Approaches

Both approaches fill an n × n table. The first works on substrings directly; the second reuses the longest common subsequence algorithm.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n is the length of the string. Both approaches compute n² table entries in constant time each; the LCS version stores only one row at a time.

Interview Tips and Recommended Approach

  • Distinguish subsequence from substring. The longest palindromic substring is a different problem with different solutions.
  • Explain the fill order. Each cell needs shorter intervals, so iterate i downwards.
  • Know the deletion connection. The fewest deletions to make a string a palindrome is n minus this answer.
  • Mention the memory optimisation when the interviewer asks about long strings.

Recommended Approach: Interval DP

Why: it states the recurrence directly in terms of the string's ends, which is easy to explain and to extend, for example to reconstruct the subsequence itself.