Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int longestCommonSubsequence(String text1, String text2) {
- int[][] dp = new int[text1.length()][text2.length()];
- // when 0,0
- char c_1 = text1.charAt(0);
- char c_2 = text2.charAt(0);
- if (c_1 == c_2) {
- dp[0][0] = 1;
- }else {
- dp[0][0] = 0;
- }
- // when text1 is 0
- for (int i = 1; i < text2.length() ;i++) {
- char c1 = text1.charAt(0);
- char c2 = text2.charAt(i);
- Integer result_max = Integer.MIN_VALUE;
- Integer result_1 = Integer.MIN_VALUE;
- Integer result_2 = Integer.MIN_VALUE;
- if (c1 == c2) {
- result_1 = 1;
- }
- result_2 = dp[0][i - 1];
- result_max = Math.max(result_max, result_1);
- result_max = Math.max(result_max, result_2);
- dp[0][i] = result_max;
- }
- // when text2 is 0
- for (int i = 1; i < text1.length() ;i++) {
- char c1 = text1.charAt(i);
- char c2 = text2.charAt(0);
- Integer result_max = Integer.MIN_VALUE;
- Integer result_1 = Integer.MIN_VALUE;
- Integer result_2 = Integer.MIN_VALUE;
- if (c1 == c2) {
- result_1 = 1;
- }
- result_2 = dp[i-1][0];
- result_max = Math.max(result_max, result_1);
- result_max = Math.max(result_max, result_2);
- dp[i][0] = result_max;
- }
- // go
- for (int i = 1; i < text1.length() ;i++) {
- char c1 = text1.charAt(i);
- System.out.printf("");
- for (int j = 1; j < text2.length() ;j++) {
- char c2 = text2.charAt(j);
- Integer result_max = Integer.MIN_VALUE;
- Integer result_1 = Integer.MIN_VALUE;
- Integer result_2 = Integer.MIN_VALUE;
- Integer result_3 = Integer.MIN_VALUE;
- if (c1 == c2) {
- // option1
- result_1 = 1 + dp[i-1][j-1];
- }
- // option2
- result_2 = dp[i-1][j];
- // option3
- result_3 = dp[i][j-1];
- result_max = Math.max(result_max, result_1);
- result_max = Math.max(result_max, result_2);
- result_max = Math.max(result_max, result_3);
- dp[i][j] = result_max;
- }
- }
- return dp[text1.length() - 1][text2.length() - 1];
- // Integer result = go(text1, text2, 0, 0);
- // return result;
- }
- Map<String, Integer> cached = new HashMap<>();
- public String get_key(int i1, int i2) {
- return i1 + "," + i2;
- }
- public Integer go(String text1, String text2, int i1, int i2) {
- if (i1 == text1.length() || i2 == text2.length()) return 0;
- String key = get_key(i1,i2);
- if (cached.containsKey(key)) {
- return cached.get(key);
- }
- char c1 = text1.charAt(i1);
- char c2 = text2.charAt(i2);
- Integer result_max = Integer.MIN_VALUE;
- Integer result_1 = Integer.MIN_VALUE;
- Integer result_2 = Integer.MIN_VALUE;
- Integer result_3 = Integer.MIN_VALUE;
- if (c1 == c2) {
- // option1
- result_1 = 1 + go(text1, text2, i1 + 1, i2 + 1);
- }
- // option2
- result_2 = go(text1, text2, i1 + 1, i2);
- // option3
- result_3 = go(text1, text2, i1, i2 + 1);
- result_max = Math.max(result_max, result_1);
- result_max = Math.max(result_max, result_2);
- result_max = Math.max(result_max, result_3);
- key = get_key(i1,i2);
- cached.put(key, result_max);
- return result_max;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement