-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1092.最短公共超序列.js
68 lines (63 loc) · 1.78 KB
/
1092.最短公共超序列.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* @lc app=leetcode.cn id=1092 lang=javascript
*
* [1092] 最短公共超序列
*
* 1. 求最长公共子序列
* 2. 基于公共子序列构建超序列, 遍历公共子序列
* 如果 str1 和 str2 当前字符与子序列当前字符不相符, 则按顺序插入 str1 和 str2 中多的字符到结果中, 直到 str1 和 str2 当前字符与子序列当前字符相符
* 如果相符, 则将当前字符插入结果中, 子序列、str1、str2 同时前进一位
*/
// @lc code=start
/**
* @param {string} str1
* @param {string} str2
* @return {string}
*/
var shortestCommonSupersequence = function(str1, str2) {
const dp = [];
let lcs = '';
for (let i = 0; i <= str1.length; i++) {
dp[i] = [];
for (let j = 0; j <= str2.length; j++) {
if (i === 0 || j === 0) {
dp[i][j] = '';
} else {
if (str1[i - 1] === str2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + str1[i - 1];
} else {
dp[i][j] = dp[i - 1][j].length > dp[i][j - 1].length ? dp[i - 1][j] : dp[i][j - 1];
}
}
if (dp[i][j].length > lcs.length) {
lcs = dp[i][j]
}
}
}
let result = [];
let a = 0;
let b = 0;
for (let i = 0; i < lcs.length; i++) {
while (str1[a] !== lcs[i]) {
result.push(str1[a]);
a++;
}
while (str2[b] !== lcs[i]) {
result.push(str2[b]);
b++;
}
result.push(lcs[i]);
a++;
b++;
}
while (a < str1.length) {
result.push(str1[a]);
a++;
}
while (b < str2.length) {
result.push(str2[b]);
b++;
}
return result.join('');
};
// @lc code=end