data-structures · intermediate · ~15 min
Find the LCS length of two strings.
Implement:
int lcs(const char *a, const char *b);
Return the length of the Longest Common Subsequence (characters in order, not necessarily contiguous) of a and b.
Two strings.
LCS length.
Use a 2-D table of size (|a|+1)x(|b|+1).
#include <stddef.h>
/* Length of the Longest Common Subsequence of strings a and b. */
int lcs(const char *a,const char *b){ (void)a;(void)b; return 0; }
Confusing subsequence (non-contiguous) with substring (contiguous).
Empty string -> 0.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.