From 48c6ae3f8255d4eabbcac4c189cea4c488d72c24 Mon Sep 17 00:00:00 2001 From: rain84 Date: Fri, 14 Jun 2024 01:47:17 +0300 Subject: [PATCH] feat: add ts solution to lc problem: No.0127 --- solution/0100-0199/0127.Word Ladder/README.md | 51 +++++++++++++++++++ .../0100-0199/0127.Word Ladder/README_EN.md | 51 +++++++++++++++++++ .../0100-0199/0127.Word Ladder/Solution.ts | 46 +++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 solution/0100-0199/0127.Word Ladder/Solution.ts diff --git a/solution/0100-0199/0127.Word Ladder/README.md b/solution/0100-0199/0127.Word Ladder/README.md index 6551ffc89ddb6..ef4ace621e8a5 100644 --- a/solution/0100-0199/0127.Word Ladder/README.md +++ b/solution/0100-0199/0127.Word Ladder/README.md @@ -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 = {}; + const g: Record = {}; + + 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([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; +} +``` + diff --git a/solution/0100-0199/0127.Word Ladder/README_EN.md b/solution/0100-0199/0127.Word Ladder/README_EN.md index db2d6c3b46c5a..7a48afc4b7d29 100644 --- a/solution/0100-0199/0127.Word Ladder/README_EN.md +++ b/solution/0100-0199/0127.Word Ladder/README_EN.md @@ -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 = {}; + const g: Record = {}; + + 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([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; +} +``` + diff --git a/solution/0100-0199/0127.Word Ladder/Solution.ts b/solution/0100-0199/0127.Word Ladder/Solution.ts new file mode 100644 index 0000000000000..45b95b665ae2c --- /dev/null +++ b/solution/0100-0199/0127.Word Ladder/Solution.ts @@ -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 = {}; + const g: Record = {}; + + 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([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; +}