/**
* @param {string} text1
* @param {string} text2
* @return {number}
*/
var longestCommonSubsequence = function(text1, text2) {
const m = new Array(text1.length + 1);
for (let i = 0; i < text1.length + 1; i++) {
m[i] = new Array(text2.length + 1).fill(0);
}
for (let i = 1; i < text1.length + 1; i++) {
const c1 = text1.charAt(i - 1);
for (let j = 1; j < text2.length + 1; j++) {
const c2 = text2.charAt(j - 1);
if (c1 === c2) {
m[i][j] = 1 + m[i - 1][j - 1];
} else {
m[i][j] = Math.max(m[i - 1][j], m[i][j - 1]);
}
}
}
return m[text1.length][text2.length];
};