1143. Longest Common Subsequence

# Medium

Solution:

  1. Initialize maxCommon[i][j], which means longest common substring between text1[:i] and text2[:j]. Empty element is necessarily needed

  2. Three cases:

    1. maxCommon[i-1][j]. eg, abc+d and ace

    2. maxCommon[i][j-1]. eg, abc and ac+d

    3. If text1[i] = text2[j], then maxCommon[i][j] + 1 common char. eg, abc+d and ace+d

    Compare to assign biggest number to maxCommon[i][j]

Last updated

Was this helpful?