-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path1092-shortest-common-supersequence.js
50 lines (45 loc) · 1.39 KB
/
1092-shortest-common-supersequence.js
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
/**
* 1092. Shortest Common Supersequence
* https://leetcode.com/problems/shortest-common-supersequence/
* Difficulty: Hard
*
* Given two strings str1 and str2, return the shortest string that has both str1 and str2 as
* subsequences. If there are multiple valid strings, return any of them.
*
* A string s is a subsequence of string t if deleting some number of characters from t (possibly
* 0) results in the string s.
*/
/**
* @param {string} str1
* @param {string} str2
* @return {string}
*/
var shortestCommonSupersequence = function(str1, str2) {
const dp = new Array(str1.length + 1).fill().map(() => new Array(str2.length + 1).fill(0));
let result = '';
for (let i = 0; i <= str1.length; i++) {
dp[i][0] = i;
}
for (let j = 0; j <= str2.length; j++) {
dp[0][j] = j;
}
for (let i = 1; i <= str1.length; i++) {
for (let j = 1; j <= str2.length; j++) {
dp[i][j] = str1[i-1] === str2[j-1] ? dp[i-1][j-1] + 1 : Math.min(dp[i-1][j], dp[i][j-1]) + 1;
}
}
for (let i = str1.length, j = str2.length; i > 0 || j > 0;) {
if (!i) {
result = str2[--j] + result;
} else if (!j) {
result = str1[--i] + result;
} else if (str1[i-1] === str2[j-1]) {
result = str1[--i] + (str2[--j], result);
} else {
dp[i][j] === dp[i-1][j] + 1
? result = str1[--i] + result
: result = str2[--j] + result;
}
}
return result;
};