Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Complete the commonChild function below.
- static int commonChild(String s1, String s2) {
- int N = s1.length();
- int[][] cache = new int[N+1][N+1];
- for (int i = 0; i < N+1; i++)
- Arrays.fill(cache[i], 0);
- for (int i1 = N-1; i1 >= 0; i1--) {
- for (int i2 = N-1; i2 >= 0; i2--) {
- if (s1.charAt(i1) == s2.charAt(i2)) {
- cache[i1][i2] = 1 + cache[i1+1][i2+1];
- }
- else cache[i1][i2] = Math.max(cache[i1][i2+1], cache[i1+1][i2]);
- }
- }
- return cache[0][0];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement