I have a DP based implementation for the Longest Common Subsequence(LCS) problem to find the length of the LCS.
I wanted to know if there is room for improvement in terms of efficiency(space/time).
public int longestCommonSubsequence(char[] firstWord, char[] secondWord) {
int[][] lcsMatrix = new int[firstWord.length+1][secondWord.length+1];
for(int i = 0; i < firstWord.length; i++) {
for(int j = 0; j < secondWord.length ; j++) {
if (i == 0 || j == 0) {
lcsMatrix[i][j] = 0;
}
else if(firstWord[i] == secondWord[j]){
lcsMatrix[i][j] = lcsMatrix[i-1][j-1]+1;
}
else{
lcsMatrix[i][j] = Math.max(lcsMatrix[i][j-1],lcsMatrix[i-1][j]);
}
}
}
return lcsMatrix[firstWord.length-1][secondWord.length-1];
}