Advertisement
uopspop

Untitled

Oct 2nd, 2021
1,253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 KB | None | 0 0
  1. class Solution {
  2.     public int longestCommonSubsequence(String text1, String text2) {
  3.        
  4.         int[][] dp = new int[text1.length()][text2.length()];
  5.        
  6.         // when 0,0
  7.         char c_1 = text1.charAt(0);
  8.         char c_2 = text2.charAt(0);
  9.         if (c_1 == c_2) {
  10.             dp[0][0] = 1;
  11.         }else {
  12.             dp[0][0] = 0;
  13.         }
  14.        
  15.         // when text1 is 0
  16.         for (int i = 1; i < text2.length() ;i++) {
  17.             char c1 = text1.charAt(0);
  18.             char c2 = text2.charAt(i);
  19.            
  20.             Integer result_max = Integer.MIN_VALUE;
  21.             Integer result_1 = Integer.MIN_VALUE;
  22.             Integer result_2 = Integer.MIN_VALUE;            
  23.            
  24.             if (c1 == c2) {
  25.                 result_1 = 1;
  26.             }
  27.             result_2 = dp[0][i - 1];
  28.            
  29.             result_max = Math.max(result_max, result_1);
  30.             result_max = Math.max(result_max, result_2);
  31.            
  32.             dp[0][i] = result_max;            
  33.         }
  34.         // when text2 is 0
  35.         for (int i = 1; i < text1.length() ;i++) {
  36.             char c1 = text1.charAt(i);
  37.             char c2 = text2.charAt(0);
  38.            
  39.             Integer result_max = Integer.MIN_VALUE;
  40.             Integer result_1 = Integer.MIN_VALUE;
  41.             Integer result_2 = Integer.MIN_VALUE;            
  42.            
  43.             if (c1 == c2) {
  44.                 result_1 = 1;
  45.             }
  46.             result_2 = dp[i-1][0];
  47.            
  48.             result_max = Math.max(result_max, result_1);
  49.             result_max = Math.max(result_max, result_2);
  50.            
  51.             dp[i][0] = result_max;
  52.         }
  53.         // go
  54.         for (int i = 1; i < text1.length() ;i++) {
  55.             char c1 = text1.charAt(i);
  56.             System.out.printf("");
  57.             for (int j = 1; j < text2.length() ;j++) {
  58.                
  59.                 char c2 = text2.charAt(j);
  60.                
  61.                 Integer result_max = Integer.MIN_VALUE;
  62.                 Integer result_1 = Integer.MIN_VALUE;
  63.                 Integer result_2 = Integer.MIN_VALUE;
  64.                 Integer result_3 = Integer.MIN_VALUE;
  65.                 if (c1 == c2) {
  66.                     // option1
  67.                     result_1 = 1 + dp[i-1][j-1];      
  68.                 }
  69.  
  70.                 // option2
  71.                 result_2 = dp[i-1][j];
  72.                 // option3
  73.                 result_3 = dp[i][j-1];
  74.  
  75.                 result_max = Math.max(result_max, result_1);
  76.                 result_max = Math.max(result_max, result_2);
  77.                 result_max = Math.max(result_max, result_3);
  78.                
  79.                 dp[i][j] = result_max;
  80.             }
  81.         }
  82.        
  83.         return dp[text1.length() - 1][text2.length() - 1];
  84.        
  85.         // Integer result = go(text1, text2, 0, 0);
  86.         // return result;
  87.     }
  88.    
  89.     Map<String, Integer> cached = new HashMap<>();
  90.     public String get_key(int i1, int i2) {
  91.         return i1 + "," + i2;
  92.     }
  93.     public Integer go(String text1, String text2, int i1, int i2) {
  94.         if (i1 == text1.length() || i2 == text2.length()) return 0;
  95.        
  96.         String key = get_key(i1,i2);
  97.         if (cached.containsKey(key)) {
  98.             return cached.get(key);
  99.         }
  100.        
  101.         char c1 = text1.charAt(i1);
  102.         char c2 = text2.charAt(i2);
  103.        
  104.         Integer result_max = Integer.MIN_VALUE;
  105.         Integer result_1 = Integer.MIN_VALUE;
  106.         Integer result_2 = Integer.MIN_VALUE;
  107.         Integer result_3 = Integer.MIN_VALUE;
  108.         if (c1 == c2) {
  109.             // option1
  110.             result_1 = 1 + go(text1, text2, i1 + 1, i2 + 1);            
  111.         }
  112.        
  113.         // option2
  114.         result_2 = go(text1, text2, i1 + 1, i2);
  115.         // option3
  116.         result_3 = go(text1, text2, i1, i2 + 1);
  117.        
  118.         result_max = Math.max(result_max, result_1);
  119.         result_max = Math.max(result_max, result_2);
  120.         result_max = Math.max(result_max, result_3);
  121.        
  122.         key = get_key(i1,i2);
  123.         cached.put(key, result_max);
  124.        
  125.         return result_max;
  126.        
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement