data-structures · intermediate · ~15 min
Find the longest contiguous common substring.
Implement:
int longest_common_substring(const char *a, const char *b);
Return the length of the longest contiguous substring common to a and b.
Two strings.
Longest common substring length.
2-D table; reset to 0 on mismatch.
#include <stddef.h>
/* Length of the Longest Common (contiguous) Substring of a and b. */
int longest_common_substring(const char *a,const char *b){ (void)a;(void)b; return -1; }
Carrying the diagonal value through a mismatch (that computes subsequence, not substring).
No common characters -> 0.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.