-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathShortest_Common_Supersequence.java
50 lines (40 loc) · 1.18 KB
/
Shortest_Common_Supersequence.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// { Driver Code Starts
//Initial Template for Java
/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args) {
// taking input using Scanner class
Scanner sc = new Scanner(System.in);
// taking total testcases
int t = sc.nextInt();
sc.nextLine();
while (t-- > 0) {
// taking String X and Y
String S[] = sc.nextLine().split(" ");
String X = S[0];
String Y = S[1];
// calling function shortestCommonSupersequence()
System.out.println(new Solution().shortestCommonSupersequence(X, Y, X.length(), Y.length()));
}
}
}
class Solution {
public static int shortestCommonSupersequence(String X, String Y, int m, int n) {
return m + n - LCS(X, Y, m, n);
}
public static int LCS(String s, String t, int n, int m) {
int[][] dp = new int[n + 1][m + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s.charAt(i - 1) == t.charAt(j - 1)) {
dp[i][j] = 1 + dp[i - 1][j - 1];
} else {
dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
}
}
}
return dp[n][m];
}
}