• + 0 comments

    Another word for this is "longest common subsequence" (or lcs). The basic algorithm is

    int lcs(char *s1, int i, char *s2, int j) {
        if (i == s1.length || j == s2.length) {
            return 0;
        }
        if (s1[i] == s2[j]) {
            return 1 + lcs(s1, i+1, s2, j+1);
        }
        return max(lcs(s1, i+1, s2, j), lcs(s1, i, s2, j+1);
    }
    

    Called from main with

    return lcs(s1, 0, s2, 0);
    

    Of course, you will need to use memoization to avoid an exponential run time.