Skip to content

Commit 93f3b3a

Browse files
committed
feat: add solutions to lc problem: No.1092
No.1092.Shortest Common Supersequence
1 parent 42a5bae commit 93f3b3a

File tree

3 files changed

+132
-1
lines changed

3 files changed

+132
-1
lines changed

solution/1000-1099/1092.Shortest Common Supersequence/README.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,26 @@ $$
5252

5353
接下来我们基于 $f[i][j]$ 构造出最短公共超序列。
5454

55-
用双指针 $i$ 和 $j$ 分别指向字符串 $str1$ 和 $str2$ 的末尾,然后从后往前遍历,每次比较 $str1[i]$ 和 $str2[j]$ 的值,如果 $str1[i] = str2[j]$,则将 $str1[i]$ 或 $str2[j]$ 中的任意一个字符加入到最短公共超序列的末尾,然后 $i$ 和 $j$ 同时减 1;如果 $str1[i] \neq str2[j]$,则将 $f[i][j]$ 与 $f[i - 1][j]$ 和 $f[i][j - 1]$ 中的最大值进行比较,如果 $f[i][j] = f[i - 1][j]$,则将 $str1[i]$ 加入到最短公共超序列的末尾,然后 $i$ 减 1;如果 $f[i][j] = f[i][j - 1]$,则将 $str2[j]$ 加入到最短公共超序列的末尾,然后 $j$ 减 1。重复上述操作,直到 $i = 0$ 或 $j = 0$,然后将剩余的字符串加入到最短公共超序列的末尾即可。
55+
```bash
56+
str1: a b a c
57+
58+
str2: c a b
59+
60+
ans: c a b a c
61+
```
62+
63+
不妨对照着上面的示例字符串,来看看如何构造出最短公共超序列。
64+
65+
我们用双指针 $i$ 和 $j$ 分别指向字符串 $str1$ 和 $str2$ 的末尾,然后从后往前遍历,每次比较 $str1[i]$ 和 $str2[j]$ 的值:
66+
67+
- 如果 $str1[i] = str2[j]$,则将 $str1[i]$ 或 $str2[j]$ 中的任意一个字符加入到最答案序列的末尾,然后 $i$ 和 $j$ 同时减 $1$;
68+
- 如果 $str1[i] \neq str2[j]$,则将 $f[i][j]$ 与 $f[i - 1][j]$ 和 $f[i][j - 1]$ 中的最大值进行比较:
69+
- 如果 $f[i][j] = f[i - 1][j]$,则将 $str1[i]$ 加入到答案序列的末尾,然后 $i$ 减 $1$;
70+
- 如果 $f[i][j] = f[i][j - 1]$,则将 $str2[j]$ 加入到答案序列的末尾,然后 $j$ 减 $1$。
71+
72+
重复上述操作,直到 $i = 0$ 或 $j = 0$,然后将剩余的字符串加入到答案序列的末尾即可。
73+
74+
最后我们将答案序列反转,即可得到最终的答案。
5675

5776
时间复杂度 $O(m\times n)$,空间复杂度 $O(m\times n)$。其中 $m$ 和 $n$ 分别是字符串 $str1$ 和 $str2$ 的长度。
5877

@@ -228,6 +247,45 @@ func max(a, b int) int {
228247
}
229248
```
230249

250+
### **TypeScript**
251+
252+
```ts
253+
function shortestCommonSupersequence(str1: string, str2: string): string {
254+
const m = str1.length;
255+
const n = str2.length;
256+
const f = new Array(m + 1).fill(0).map(() => new Array(n + 1).fill(0));
257+
for (let i = 1; i <= m; ++i) {
258+
for (let j = 1; j <= n; ++j) {
259+
if (str1[i - 1] == str2[j - 1]) {
260+
f[i][j] = f[i - 1][j - 1] + 1;
261+
} else {
262+
f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]);
263+
}
264+
}
265+
}
266+
let ans: string[] = [];
267+
let i = m;
268+
let j = n;
269+
while (i > 0 || j > 0) {
270+
if (i === 0) {
271+
ans.push(str2[--j]);
272+
} else if (j === 0) {
273+
ans.push(str1[--i]);
274+
} else {
275+
if (f[i][j] === f[i - 1][j]) {
276+
ans.push(str1[--i]);
277+
} else if (f[i][j] === f[i][j - 1]) {
278+
ans.push(str2[--j]);
279+
} else {
280+
ans.push(str1[--i]);
281+
--j;
282+
}
283+
}
284+
}
285+
return ans.reverse().join('');
286+
}
287+
```
288+
231289
### **...**
232290

233291
```

solution/1000-1099/1092.Shortest Common Supersequence/README_EN.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,45 @@ func max(a, b int) int {
205205
}
206206
```
207207

208+
### **TypeScript**
209+
210+
```ts
211+
function shortestCommonSupersequence(str1: string, str2: string): string {
212+
const m = str1.length;
213+
const n = str2.length;
214+
const f = new Array(m + 1).fill(0).map(() => new Array(n + 1).fill(0));
215+
for (let i = 1; i <= m; ++i) {
216+
for (let j = 1; j <= n; ++j) {
217+
if (str1[i - 1] == str2[j - 1]) {
218+
f[i][j] = f[i - 1][j - 1] + 1;
219+
} else {
220+
f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]);
221+
}
222+
}
223+
}
224+
let ans: string[] = [];
225+
let i = m;
226+
let j = n;
227+
while (i > 0 || j > 0) {
228+
if (i === 0) {
229+
ans.push(str2[--j]);
230+
} else if (j === 0) {
231+
ans.push(str1[--i]);
232+
} else {
233+
if (f[i][j] === f[i - 1][j]) {
234+
ans.push(str1[--i]);
235+
} else if (f[i][j] === f[i][j - 1]) {
236+
ans.push(str2[--j]);
237+
} else {
238+
ans.push(str1[--i]);
239+
--j;
240+
}
241+
}
242+
}
243+
return ans.reverse().join('');
244+
}
245+
```
246+
208247
### **...**
209248

210249
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function shortestCommonSupersequence(str1: string, str2: string): string {
2+
const m = str1.length;
3+
const n = str2.length;
4+
const f = new Array(m + 1).fill(0).map(() => new Array(n + 1).fill(0));
5+
for (let i = 1; i <= m; ++i) {
6+
for (let j = 1; j <= n; ++j) {
7+
if (str1[i - 1] == str2[j - 1]) {
8+
f[i][j] = f[i - 1][j - 1] + 1;
9+
} else {
10+
f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]);
11+
}
12+
}
13+
}
14+
let ans: string[] = [];
15+
let i = m;
16+
let j = n;
17+
while (i > 0 || j > 0) {
18+
if (i === 0) {
19+
ans.push(str2[--j]);
20+
} else if (j === 0) {
21+
ans.push(str1[--i]);
22+
} else {
23+
if (f[i][j] === f[i - 1][j]) {
24+
ans.push(str1[--i]);
25+
} else if (f[i][j] === f[i][j - 1]) {
26+
ans.push(str2[--j]);
27+
} else {
28+
ans.push(str1[--i]);
29+
--j;
30+
}
31+
}
32+
}
33+
return ans.reverse().join('');
34+
}

0 commit comments

Comments
 (0)