Skip to content

Commit a7dd169

Browse files
committed
https://leetcode.cn/problems/word-ladder/
1 parent 91a8ff5 commit a7dd169

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
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/word-ladder/
14+
1315
https://leetcode.cn/problems/add-one-row-to-tree/
1416

1517
https://leetcode.cn/problems/minimum-subsequence-in-non-increasing-order/

add-one-row-to-tree/test.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { assertEquals } from "../deps.ts";
2+
import { TreeNode } from "../mod.ts";
3+
import addOneRow from "./index.ts";
4+
5+
Deno.test("add-one-row-to-tree", () => {
6+
const outputs = [
7+
{
8+
val: 4,
9+
left: {
10+
val: 1,
11+
left: {
12+
val: 2,
13+
left: { val: 3, left: null, right: null },
14+
right: { val: 1, left: null, right: null },
15+
},
16+
right: null,
17+
},
18+
right: {
19+
val: 1,
20+
left: null,
21+
right: {
22+
val: 6,
23+
left: { val: 5, left: null, right: null },
24+
right: null,
25+
},
26+
},
27+
},
28+
{
29+
val: 4,
30+
left: {
31+
val: 2,
32+
left: {
33+
val: 1,
34+
left: { val: 3, left: null, right: null },
35+
right: null,
36+
},
37+
right: {
38+
val: 1,
39+
left: null,
40+
right: { val: 1, left: null, right: null },
41+
},
42+
},
43+
right: null,
44+
},
45+
];
46+
const inputs = [
47+
[
48+
{
49+
val: 4,
50+
left: {
51+
val: 2,
52+
left: { val: 3, left: null, right: null },
53+
right: { val: 1, left: null, right: null },
54+
},
55+
right: {
56+
val: 6,
57+
left: { val: 5, left: null, right: null },
58+
right: null,
59+
},
60+
},
61+
1,
62+
2,
63+
],
64+
[
65+
{
66+
val: 4,
67+
left: {
68+
val: 2,
69+
left: { val: 3, left: null, right: null },
70+
right: { val: 1, left: null, right: null },
71+
},
72+
right: null,
73+
},
74+
1,
75+
3,
76+
],
77+
] as Array<[root: TreeNode | null, val: number, depth: number]>;
78+
assertEquals(
79+
outputs,
80+
inputs.map((args) =>
81+
structuredClone(Reflect.apply(addOneRow, null, args))
82+
),
83+
);
84+
});

word-ladder/index.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
export default function ladderLength(
2+
beginWord: string,
3+
endWord: string,
4+
wordList: string[],
5+
): number {
6+
const words = new Set(wordList);
7+
if (!words.has(endWord)) return 0;
8+
9+
if (beginWord.length === 1) return 2;
10+
11+
let current = new Set([beginWord]);
12+
13+
let rightcurrent = new Set([endWord]);
14+
words.delete(endWord);
15+
let step = 1;
16+
while (current.size) {
17+
if (current.size > rightcurrent.size) {
18+
[current, rightcurrent] = [rightcurrent, current];
19+
}
20+
21+
const temp: Set<string> = new Set();
22+
23+
for (const word of current) {
24+
for (const right of rightcurrent) {
25+
if (diffonechar(word, right)) {
26+
return step + 1;
27+
}
28+
}
29+
for (const other of words) {
30+
if (diffonechar(other, word)) {
31+
temp.add(other);
32+
33+
words.delete(other);
34+
}
35+
}
36+
}
37+
38+
if (temp.size === 0) return 0;
39+
current = temp;
40+
step = step + 1;
41+
}
42+
43+
return 0;
44+
}
45+
46+
function diffonechar(word1: string, word2: string): boolean {
47+
let changes = 0;
48+
for (let i = 0; i < word1.length; i++) {
49+
if (word1[i] != word2[i]) changes += 1;
50+
}
51+
return changes === 1;
52+
}

word-ladder/test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
2+
import ladderLength from "./index.ts";
3+
4+
Deno.test("word-ladder", () => {
5+
const beginWord = "hit",
6+
endWord = "cog",
7+
wordList = ["hot", "dot", "dog", "lot", "log", "cog"];
8+
assertEquals(5, ladderLength(beginWord, endWord, wordList));
9+
});
10+
Deno.test("word-ladder", () => {
11+
const beginWord = "hit",
12+
endWord = "cog",
13+
wordList = ["hot", "dot", "dog", "lot", "log"];
14+
assertEquals(0, ladderLength(beginWord, endWord, wordList));
15+
});
16+
Deno.test("word-ladder", () => {
17+
assertEquals(2, ladderLength("a", "c", ["a", "b", "c"]));
18+
});
19+
Deno.test("word-ladder", () => {
20+
assertEquals(0, ladderLength("hot", "dog", ["hot", "dog"]));
21+
});

0 commit comments

Comments
 (0)