Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. // Complete the commonChild function below.
  2. static int commonChild(String s1, String s2) {
  3. int N = s1.length();
  4. int[][] cache = new int[N+1][N+1];
  5. for (int i = 0; i < N+1; i++)
  6. Arrays.fill(cache[i], 0);
  7.  
  8. for (int i1 = N-1; i1 >= 0; i1--) {
  9. for (int i2 = N-1; i2 >= 0; i2--) {
  10.  
  11. if (s1.charAt(i1) == s2.charAt(i2)) {
  12. cache[i1][i2] = 1 + cache[i1+1][i2+1];
  13. }
  14. else cache[i1][i2] = Math.max(cache[i1][i2+1], cache[i1+1][i2]);
  15.  
  16. }
  17. }
  18. return cache[0][0];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement