-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
26 lines (23 loc) · 986 Bytes
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.company;
public class Main {
public static void main(String[] args) {
// write your code here
}
public int longestCommonSubsequence(String text1, String text2) {
int[][] dp = new int[text1.length()][text2.length()];
dp[0][0] = text1.charAt(0) == text2.charAt(0) ? 1 : 0;
for (int i = 1; i < dp.length; i++)
dp[i][0] = text1.charAt(i) == text2.charAt(0) ? 1 : dp[i - 1][0];
for (int i = 1; i < dp[0].length; i++)
dp[0][i] = text1.charAt(0) == text2.charAt(i) ? 1 : dp[0][i - 1];
for (int i = 1; i < dp.length; i++) {
for (int j = 1; j < dp[0].length; j++) {
if (text1.charAt(i) == text2.charAt(j))
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = Math.max(dp[i - 1][j - 1], Math.max(dp[i - 1][j], dp[i][j - 1]));
}
}
return dp[text1.length() - 1][text2.length() - 1];
}
}