data-structures · intermediate · ~15 min

Longest palindromic subsequence

Find the longest palindromic subsequence length.

Challenge

Implement:

int lps(const char *s);

Return the length of the Longest Palindromic Subsequence of s.

Input format

A string.

Output format

LPS length.

Constraints

Interval DP over substrings (i..j).

Starter code

#include <stddef.h>
/* Length of the Longest Palindromic Subsequence of s. */
int lps(const char *s){ (void)s; return 0; }

Common mistakes

Iterating in the wrong order — inner substrings must be solved before outer ones.

Edge cases to handle

Single char -> 1; empty -> 0.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.