-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathSmallest Super-Sequence
76 lines (63 loc) · 1.98 KB
/
Smallest Super-Sequence
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Given two strings S and T with lengths M and N. Find and return the length of their shortest 'Super Sequence'.
The shortest 'Super Sequence' of two strings is defined as the smallest string possible that will have both the given strings as its subsequences.
Note : If the two strings do not have any common characters, then return the sum of the lengths of the two strings.
Input Format:
The first only line of input contains a string, that denotes the value of string S. The following line contains a string, that denotes the value of string T.
Output Format:
Length of the smallest super-sequence of given two strings.
Constraints :
0 <= M <= 10 ^ 3
0 <= N <= 10 ^ 3
Time Limit: 1 sec
Sample Input 1 :
ab
ac
Sample Output 1 :
3
Explanation of Sample Output 1 :
Their smallest super sequence can be "abc" which has length = 3.
Sample Input 2 :
pqqrpt
qerepct
Sample Output 2 :
9
*/
public class Solution {
public static int smallestSuperSequence(String str1, String str2) {
/* Your class should be named Solution
* Don't write main().
* Don't read input, it is passed as function argument.
* Return output and don't print it.
* Taking input and printing output is handled automatically.
*/
int n1=str1.length();
int n2=str2.length();
int[][] dp = new int[n1+1][n2+1];
for (int i=n1;i>=0;i--)
{
dp[i][n2]=n1-i;
}
for (int i=n2;i>=0;i--)
{
dp[n1][i]=n2-i;
}
for (int i=n1-1;i>=0;i--)
{
for (int j=n2-1;j>=0;j--)
{
if(str1.charAt(i)==str2.charAt(j))
{
dp[i][j]=dp[i+1][j+1]+1;
}
else
{
int ans1=dp[i+1][j];
int ans2=dp[i][j+1];
dp[i][j]=Math.min(ans1,ans2)+1;
}
}
}
return dp[0][0];
}
}