Skip to content

Commit 2ea4252

Browse files
committed
https://leetcode.cn/problems/longest-string-chain/
1 parent d1abdc6 commit 2ea4252

File tree

3 files changed

+91
-46
lines changed

3 files changed

+91
-46
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ leetcode 测试
1010

1111
##### 包含的内容如下
1212

13+
https://leetcode.cn/problems/longest-string-chain/
14+
1315
https://leetcode.cn/problems/add-two-polynomials-represented-as-linked-lists/
1416

1517
https://leetcode.cn/problems/print-immutable-linked-list-in-reverse/
Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
1-
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
2-
import addPoly from "./index.ts";
3-
import { ArrayToPolyNode } from "./ArrayToPolyNode.ts";
4-
import { PolyNodeToArray } from "./PolyNodeToArray.ts";
5-
Deno.test("add-two-polynomials-represented-as-linked-lists", () => {
6-
const cases = [
7-
[[[1, 2]], [[-1, 2]], []],
8-
[
9-
[
10-
[2, 2],
11-
[4, 1],
12-
[3, 0],
13-
],
14-
15-
[
16-
[3, 2],
17-
[-4, 1],
18-
[-1, 0],
19-
],
20-
[
21-
[5, 2],
22-
[2, 0],
23-
],
24-
],
25-
[
26-
[[1, 1]],
27-
[[1, 0]],
28-
[
29-
[1, 1],
30-
[1, 0],
31-
],
32-
],
33-
];
34-
35-
cases.forEach(([a, b, c]) => {
36-
assertEquals(
37-
c,
38-
PolyNodeToArray(
39-
addPoly(
40-
ArrayToPolyNode(a as [number, number][]),
41-
ArrayToPolyNode(b as [number, number][])
42-
)
43-
)
44-
);
45-
});
46-
});
1+
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
2+
import addPoly from "./index.ts";
3+
import { ArrayToPolyNode } from "./ArrayToPolyNode.ts";
4+
import { PolyNodeToArray } from "./PolyNodeToArray.ts";
5+
Deno.test("add-two-polynomials-represented-as-linked-lists", () => {
6+
const cases = [
7+
[[[1, 2]], [[-1, 2]], []],
8+
[
9+
[
10+
[2, 2],
11+
[4, 1],
12+
[3, 0],
13+
],
14+
15+
[
16+
[3, 2],
17+
[-4, 1],
18+
[-1, 0],
19+
],
20+
[
21+
[5, 2],
22+
[2, 0],
23+
],
24+
],
25+
[
26+
[[1, 1]],
27+
[[1, 0]],
28+
[
29+
[1, 1],
30+
[1, 0],
31+
],
32+
],
33+
];
34+
35+
cases.forEach(([a, b, c]) => {
36+
assertEquals(
37+
c,
38+
PolyNodeToArray(
39+
addPoly(
40+
ArrayToPolyNode(a as [number, number][]),
41+
ArrayToPolyNode(b as [number, number][]),
42+
),
43+
),
44+
);
45+
});
46+
});

longest-string-chain/index.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
export default function longestStrChain(words: string[]): number {
2+
const map = new Map<number, Set<number>>();
3+
let res = 1;
4+
words.sort((a, b) => a.length - b.length);
5+
words.forEach((i, idx) => {
6+
const set = map.get(i.length) ?? new Set();
7+
if (!map.has(i.length)) map.set(i.length, set);
8+
set.add(idx);
9+
});
10+
11+
const dp: number[] = words.map(() => 1);
12+
for (let i = 0; i < dp.length; i++) {
13+
const set = map.get(words[i].length - 1);
14+
if (set) {
15+
for (const idx of set) {
16+
if (can(words[idx], words[i])) {
17+
dp[i] = Math.max(dp[i], dp[idx] + 1);
18+
}
19+
res = Math.max(res, dp[i]);
20+
}
21+
}
22+
}
23+
return res;
24+
}
25+
function can(s1: string, s2: string) {
26+
// abc
27+
// adbc
28+
let c1 = 0,
29+
c2 = 0;
30+
let wrong = 0;
31+
while (c1 < s1.length) {
32+
if (s1[c1] !== s2[c2]) {
33+
wrong += 1;
34+
if (wrong > 1) return false;
35+
c2 += 1;
36+
}
37+
if (s1[c1] === s2[c2]) {
38+
c1 += 1;
39+
c2 += 1;
40+
}
41+
}
42+
return true;
43+
}

0 commit comments

Comments
 (0)