Return the length of the longest common subsequence of text1 and text2, or 0 if there is none.
Each row only needs the row above it. Keep one row and remember the old diagonal value in a variable.
1function longestCommonSubsequence(a: string, b: string): number {2const dp = new Array(b.length + 1).fill(0);3for (let i = 1; i <= a.length; i++) {4let diag = 0;5for (let j = 1; j <= b.length; j++) {6const up = dp[j];7dp[j] = a[i - 1] === b[j - 1] ? diag + 1 : Math.max(up, dp[j - 1]);8diag = up;9}10}11return dp[b.length];12}
One row of zeros.
Space: play/pause · ←/→: step