Skip to content

Commit 5f3e947

Browse files
authored
feat: add ts solution to lc problem: No.0127 (doocs#3104)
1 parent 4ca5f17 commit 5f3e947

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

solution/0100-0199/0127.Word Ladder/README.md

+51
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,57 @@ public class Solution {
310310
}
311311
```
312312

313+
#### TypeScript
314+
315+
```ts
316+
function ladderLength(beginWord: string, endWord: string, wordList: string[]): number {
317+
if (!wordList.includes(endWord)) return 0;
318+
319+
const replace = (s: string, i: number, ch: string) => s.slice(0, i) + ch + s.slice(i + 1);
320+
const { length } = beginWord;
321+
const words: Record<string, string[]> = {};
322+
const g: Record<string, string[]> = {};
323+
324+
for (const w of [beginWord, ...wordList]) {
325+
const derivatives: string[] = [];
326+
327+
for (let i = 0; i < length; i++) {
328+
const nextW = replace(w, i, '*');
329+
derivatives.push(nextW);
330+
331+
g[nextW] ??= [];
332+
g[nextW].push(w);
333+
}
334+
335+
words[w] = derivatives;
336+
}
337+
338+
let ans = 0;
339+
let q = words[beginWord];
340+
const vis = new Set<string>([beginWord]);
341+
342+
while (q.length) {
343+
const nextQ: string[] = [];
344+
ans++;
345+
346+
for (const variant of q) {
347+
for (const w of g[variant]) {
348+
if (w === endWord) return ans + 1;
349+
350+
if (vis.has(w)) continue;
351+
vis.add(w);
352+
353+
nextQ.push(...words[w]);
354+
}
355+
}
356+
357+
q = nextQ;
358+
}
359+
360+
return 0;
361+
}
362+
```
363+
313364
<!-- tabs:end -->
314365

315366
<!-- solution:end -->

solution/0100-0199/0127.Word Ladder/README_EN.md

+51
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,57 @@ public class Solution {
310310
}
311311
```
312312

313+
#### TypeScript
314+
315+
```ts
316+
function ladderLength(beginWord: string, endWord: string, wordList: string[]): number {
317+
if (!wordList.includes(endWord)) return 0;
318+
319+
const replace = (s: string, i: number, ch: string) => s.slice(0, i) + ch + s.slice(i + 1);
320+
const { length } = beginWord;
321+
const words: Record<string, string[]> = {};
322+
const g: Record<string, string[]> = {};
323+
324+
for (const w of [beginWord, ...wordList]) {
325+
const derivatives: string[] = [];
326+
327+
for (let i = 0; i < length; i++) {
328+
const nextW = replace(w, i, '*');
329+
derivatives.push(nextW);
330+
331+
g[nextW] ??= [];
332+
g[nextW].push(w);
333+
}
334+
335+
words[w] = derivatives;
336+
}
337+
338+
let ans = 0;
339+
let q = words[beginWord];
340+
const vis = new Set<string>([beginWord]);
341+
342+
while (q.length) {
343+
const nextQ: string[] = [];
344+
ans++;
345+
346+
for (const variant of q) {
347+
for (const w of g[variant]) {
348+
if (w === endWord) return ans + 1;
349+
350+
if (vis.has(w)) continue;
351+
vis.add(w);
352+
353+
nextQ.push(...words[w]);
354+
}
355+
}
356+
357+
q = nextQ;
358+
}
359+
360+
return 0;
361+
}
362+
```
363+
313364
<!-- tabs:end -->
314365

315366
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
function ladderLength(beginWord: string, endWord: string, wordList: string[]): number {
2+
if (!wordList.includes(endWord)) return 0;
3+
4+
const replace = (s: string, i: number, ch: string) => s.slice(0, i) + ch + s.slice(i + 1);
5+
const { length } = beginWord;
6+
const words: Record<string, string[]> = {};
7+
const g: Record<string, string[]> = {};
8+
9+
for (const w of [beginWord, ...wordList]) {
10+
const derivatives: string[] = [];
11+
12+
for (let i = 0; i < length; i++) {
13+
const nextW = replace(w, i, '*');
14+
derivatives.push(nextW);
15+
16+
g[nextW] ??= [];
17+
g[nextW].push(w);
18+
}
19+
20+
words[w] = derivatives;
21+
}
22+
23+
let ans = 0;
24+
let q = words[beginWord];
25+
const vis = new Set<string>([beginWord]);
26+
27+
while (q.length) {
28+
const nextQ: string[] = [];
29+
ans++;
30+
31+
for (const variant of q) {
32+
for (const w of g[variant]) {
33+
if (w === endWord) return ans + 1;
34+
35+
if (vis.has(w)) continue;
36+
vis.add(w);
37+
38+
nextQ.push(...words[w]);
39+
}
40+
}
41+
42+
q = nextQ;
43+
}
44+
45+
return 0;
46+
}

0 commit comments

Comments
 (0)