You are viewing a single comment's thread. Return to all comments →
Java Solution for the same one:
int[][] mat = new int[s1.length() + 1][s2.length() + 1]; for (int i = 0; i <= s1.length(); i++) { mat[i][0] = 0; mat[0][i] = 0; } for (int i = 0; i < s1.length(); i++) { for (int j = 0; j < s2.length(); j++) { if (s1.charAt(i) == s2.charAt(j)) { mat[i + 1][j + 1] = mat[i][j] + 1; } else { mat[i + 1][j + 1] = mat[i + 1][j] > mat[i][j + 1] ? mat[i + 1][j] : mat[i][j + 1]; } } } return mat[s1.length()][s1.length()];
Seems like cookies are disabled on this browser, please enable them to open this website
Common Child
You are viewing a single comment's thread. Return to all comments →
Java Solution for the same one:
int[][] mat = new int[s1.length() + 1][s2.length() + 1]; for (int i = 0; i <= s1.length(); i++) { mat[i][0] = 0; mat[0][i] = 0; } for (int i = 0; i < s1.length(); i++) { for (int j = 0; j < s2.length(); j++) { if (s1.charAt(i) == s2.charAt(j)) { mat[i + 1][j + 1] = mat[i][j] + 1; } else { mat[i + 1][j + 1] = mat[i + 1][j] > mat[i][j + 1] ? mat[i + 1][j] : mat[i][j + 1]; } } } return mat[s1.length()][s1.length()];