Skip to content

Commit 63186d0

Browse files
authored
Merge branch 'master' into feature/climbing-stairs
2 parents 92576ea + 0161883 commit 63186d0

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Shortest Common Supersequence Solution
2+
Overview
3+
The goal of this solution is to find the shortest common supersequence (SCS) of two given strings, str1 and str2. The shortest common supersequence is the shortest string that has both str1 and str2 as subsequences.
4+
5+
Steps
6+
Create DP Table:
7+
8+
We first create a dynamic programming (DP) table dp of size (m + 1) x (n + 1), where m is the length of str1 and n is the length of str2.
9+
10+
Fill the DP Table:
11+
12+
We fill the DP table based on the following conditions:
13+
14+
If characters of str1 and str2 match, we take the diagonal value and add 1: dp[i][j] = dp[i - 1][j - 1] + 1.
15+
16+
If characters do not match, we take the maximum value from the left or the top cell: dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]).
17+
18+
Build the Shortest Common Supersequence:
19+
20+
We use a StringBuilder to construct the SCS.
21+
22+
Starting from the bottom-right corner of the DP table, we move based on the values in the DP table:
23+
24+
If characters match, we add that character to the result and move diagonally up-left.
25+
26+
If characters do not match, we move in the direction of the larger DP value (either up or left).
27+
28+
Finally, we add any remaining characters from str1 or str2.
29+
30+
Reverse the Result:
31+
32+
The result is constructed in reverse order, so we reverse it before returning.
33+
34+
Time Complexity
35+
The time complexity is O(m * n) because we traverse the DP table of size (m + 1) x (n + 1).
36+
37+
Space Complexity
38+
The space complexity is O(m * n) because we use a DP table of size (m + 1) x (n + 1).
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class Solution {
2+
public String shortestCommonSupersequence(String str1, String str2) {
3+
int m = str1.length();
4+
int n = str2.length();
5+
6+
// Create DP table
7+
int[][] dp = new int[m + 1][n + 1];
8+
9+
// Fill the DP table
10+
for (int i = 1; i <= m; i++) {
11+
for (int j = 1; j <= n; j++) {
12+
if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
13+
dp[i][j] = dp[i - 1][j - 1] + 1; // If characters match
14+
} else {
15+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); // Take max from either previous row or column
16+
}
17+
}
18+
}
19+
20+
// Build the shortest common supersequence
21+
StringBuilder result = new StringBuilder();
22+
int i = m, j = n;
23+
while (i > 0 && j > 0) {
24+
// If characters are the same, add to the result
25+
if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
26+
result.append(str1.charAt(i - 1));
27+
i--;
28+
j--;
29+
} else if (dp[i - 1][j] > dp[i][j - 1]) { // Move in the direction of the larger dp value
30+
result.append(str1.charAt(i - 1));
31+
i--;
32+
} else {
33+
result.append(str2.charAt(j - 1));
34+
j--;
35+
}
36+
}
37+
38+
// If there are any remaining characters in str1
39+
while (i > 0) {
40+
result.append(str1.charAt(i - 1));
41+
i--;
42+
}
43+
44+
// If there are any remaining characters in str2
45+
while (j > 0) {
46+
result.append(str2.charAt(j - 1));
47+
j--;
48+
}
49+
50+
// Reverse the result before returning
51+
return result.reverse().toString();
52+
}
53+
}
54+
55+
//Time Complexity : O(m*n)
56+
//Space Complexity : O(m*n)

0 commit comments

Comments
 (0)