Skip to content

feat: add ts solution to lc problem: No.0127 #3104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions solution/0100-0199/0127.Word Ladder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,57 @@ public class Solution {
}
```

#### TypeScript

```ts
function ladderLength(beginWord: string, endWord: string, wordList: string[]): number {
if (!wordList.includes(endWord)) return 0;

const replace = (s: string, i: number, ch: string) => s.slice(0, i) + ch + s.slice(i + 1);
const { length } = beginWord;
const words: Record<string, string[]> = {};
const g: Record<string, string[]> = {};

for (const w of [beginWord, ...wordList]) {
const derivatives: string[] = [];

for (let i = 0; i < length; i++) {
const nextW = replace(w, i, '*');
derivatives.push(nextW);

g[nextW] ??= [];
g[nextW].push(w);
}

words[w] = derivatives;
}

let ans = 0;
let q = words[beginWord];
const vis = new Set<string>([beginWord]);

while (q.length) {
const nextQ: string[] = [];
ans++;

for (const variant of q) {
for (const w of g[variant]) {
if (w === endWord) return ans + 1;

if (vis.has(w)) continue;
vis.add(w);

nextQ.push(...words[w]);
}
}

q = nextQ;
}

return 0;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
51 changes: 51 additions & 0 deletions solution/0100-0199/0127.Word Ladder/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,57 @@ public class Solution {
}
```

#### TypeScript

```ts
function ladderLength(beginWord: string, endWord: string, wordList: string[]): number {
if (!wordList.includes(endWord)) return 0;

const replace = (s: string, i: number, ch: string) => s.slice(0, i) + ch + s.slice(i + 1);
const { length } = beginWord;
const words: Record<string, string[]> = {};
const g: Record<string, string[]> = {};

for (const w of [beginWord, ...wordList]) {
const derivatives: string[] = [];

for (let i = 0; i < length; i++) {
const nextW = replace(w, i, '*');
derivatives.push(nextW);

g[nextW] ??= [];
g[nextW].push(w);
}

words[w] = derivatives;
}

let ans = 0;
let q = words[beginWord];
const vis = new Set<string>([beginWord]);

while (q.length) {
const nextQ: string[] = [];
ans++;

for (const variant of q) {
for (const w of g[variant]) {
if (w === endWord) return ans + 1;

if (vis.has(w)) continue;
vis.add(w);

nextQ.push(...words[w]);
}
}

q = nextQ;
}

return 0;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
46 changes: 46 additions & 0 deletions solution/0100-0199/0127.Word Ladder/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function ladderLength(beginWord: string, endWord: string, wordList: string[]): number {
if (!wordList.includes(endWord)) return 0;

const replace = (s: string, i: number, ch: string) => s.slice(0, i) + ch + s.slice(i + 1);
const { length } = beginWord;
const words: Record<string, string[]> = {};
const g: Record<string, string[]> = {};

for (const w of [beginWord, ...wordList]) {
const derivatives: string[] = [];

for (let i = 0; i < length; i++) {
const nextW = replace(w, i, '*');
derivatives.push(nextW);

g[nextW] ??= [];
g[nextW].push(w);
}

words[w] = derivatives;
}

let ans = 0;
let q = words[beginWord];
const vis = new Set<string>([beginWord]);

while (q.length) {
const nextQ: string[] = [];
ans++;

for (const variant of q) {
for (const w of g[variant]) {
if (w === endWord) return ans + 1;

if (vis.has(w)) continue;
vis.add(w);

nextQ.push(...words[w]);
}
}

q = nextQ;
}

return 0;
}