DSADPInterval

Longest Palindromic Subsequence Visualizer

What is LPS

A palindromic subsequence reads the same forwards and backwards; letters need not be contiguous. dp[i][j] is the LPS of the substring s[i..j]. Matching ends wrap a smaller palindrome; mismatch takes the better side.

  • Match: 2 + dp[i+1][j-1] (or 2 when the window is just two letters).
  • Mismatch: max(dp[i+1][j], dp[i][j-1]).
  • Same as LCS(s, reverse(s)).

Brute force approach

On interval [i, j] match the ends and shrink, or drop one end. Same intervals repeat.

If the ends match, both are kept and the inside is solved. Otherwise one end is dropped. The span (left, right) repeats. Memoization stores that span.

Try a small input

At most 5 letters. bbbab has length 4.

Answer: 0 Brute calls: 0 Memo calls: 0 Calls saved: 0
Recursive brute force
def lps(i, j):
if i == j: return 1
match both ends, or drop one end
Step 0

Memoized approach

Fill by increasing length. Match → 2 + dp[i+1][j-1], else max of the two sides. Same as LCS(s, reverse(s)).

Each state is solved once. A dashed node is a cache hit: the call returns immediately and that branch stops.

Memoized recursion
def lps(i, j, memo):
if (i, j) in memo: return memo
if i == j: return 1
memo = match or max(drop)
return memo
Step 0

How this visualizer works

String bbbab. The interval table grows to 4 (bbbb). Step through the DP table. The live code on the right is the DP recurrence — brute force is shown in Implementations below so you can see why it TLE.

Interactive DP trace

dp_table.state

Input
DP table
Cell being filled
Source / window
Already computed

current_action

Load an input, then press Play or Step.

live_trace.py — DP fill

1def lps(s):
2n, dp = len(s), [[0]*len(s) for _ in s]
3for i in range(n): dp[i][i] = 1
4for L in range(2, n+1):
5for i in range(n-L+1):
6j = i+L-1
7if s[i]==s[j]: dp[i][j] = 2 if L==2 else dp[i+1][j-1]+2
8else: dp[i][j] = max(dp[i+1][j], dp[i][j-1])
9return dp[0][n-1]

stdout — decision log

// each table write is recorded here
Speed Ready

Pseudocode & Complexity

dp_recurrence.txt

1def lps(s):
2n, dp = len(s), [[0]*len(s) for _ in s]
3for i in range(n): dp[i][i] = 1
4for L in range(2, n+1):
5for i in range(n-L+1):
6j = i+L-1
7if s[i]==s[j]: dp[i][j] = 2 if L==2 else dp[i+1][j-1]+2
8else: dp[i][j] = max(dp[i+1][j], dp[i][j-1])
9return dp[0][n-1]

complexity.md

OperationTime
Each intervalO(1)
All intervalsO(n²)
SpaceO(n²)

Brute force explores the same subproblems many times and will TLE on the hidden tests. The DP codes below reuse each answer once.

Implementations

dp.py

1def lps_brute(s, i, j):
2if i > j:
3return 0
4if i == j:
5return 1
6if s[i] == s[j]:
7return 2 + lps_brute(s, i + 1, j - 1)
8left = lps_brute(s, i + 1, j)
9right = lps_brute(s, i, j - 1)
10return max(left, right)
1def lps_dp(s):
2n = len(s)
3dp = [[0] * n for _ in range(n)]
4for i in range(n):
5dp[i][i] = 1
6for length in range(2, n + 1):
7for i in range(n - length + 1):
8j = i + length - 1
9if s[i] == s[j]:
10if length == 2:
11dp[i][j] = 2
12else:
13dp[i][j] = dp[i + 1][j - 1] + 2
14else:
15dp[i][j] = max(dp[i + 1][j], dp[i][j - 1])
16return dp[0][n - 1] if n else 0
1int lps_brute(const char *s, int i, int j) {
2int left;
3int right;
4if (i > j) {
5return 0;
6}
7if (i == j) {
8return 1;
9}
10if (s[i] == s[j]) {
11return 2 + lps_brute(s, i + 1, j - 1);
12}
13left = lps_brute(s, i + 1, j);
14right = lps_brute(s, i, j - 1);
15return left > right ? left : right;
16}
1int lps_dp(const char *s) {
2int n = (int)strlen(s);
3int dp[201][201];
4int i;
5int j;
6int length;
7if (n == 0) {
8return 0;
9}
10for (i = 0; i < n; i++) {
11for (j = 0; j < n; j++) {
12dp[i][j] = 0;
13}
14dp[i][i] = 1;
15}
16for (length = 2; length <= n; length++) {
17for (i = 0; i + length - 1 < n; i++) {
18j = i + length - 1;
19if (s[i] == s[j]) {
20dp[i][j] = length == 2 ? 2 : dp[i + 1][j - 1] + 2;
21} else if (dp[i + 1][j] > dp[i][j - 1]) {
22dp[i][j] = dp[i + 1][j];
23} else {
24dp[i][j] = dp[i][j - 1];
25}
26}
27}
28return dp[0][n - 1];
29}