Skip to content

Commit f94b6bf

Browse files
committed
feat(js): add modular solution and documentation for normal challenge 9 - Word Ladder
1 parent e2c5cdf commit f94b6bf

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Challenge Description and Solution
2+
3+
## English Version
4+
5+
### Challenge Description
6+
Given two words (start and end) and a dictionary, implement an algorithm to find the shortest transformation sequence where each word differs by exactly one character from the previous one. This challenge requires applying search techniques like BFS.
7+
8+
### Code Explanation
9+
The solution uses a breadth-first search (BFS) approach to explore all possible one-character transformations from the current word. It uses a queue to keep track of the current word and the path taken to reach it. The search continues until the end word is found or all possibilities are exhausted.
10+
11+
### Relevant Code Snippet
12+
13+
```javascript
14+
function wordLadder(beginWord, endWord, wordList) {
15+
const wordSet = new Set(wordList);
16+
if (!wordSet.has(endWord)) {
17+
return [];
18+
}
19+
20+
const queue = [[beginWord, [beginWord]]];
21+
const visited = new Set([beginWord]);
22+
23+
while (queue.length > 0) {
24+
const [currentWord, path] = queue.shift();
25+
if (currentWord === endWord) {
26+
return path;
27+
}
28+
29+
for (let i = 0; i < currentWord.length; i++) {
30+
for (let c = 97; c <= 122; c++) { // 'a' to 'z'
31+
const char = String.fromCharCode(c);
32+
const nextWord = currentWord.slice(0, i) + char + currentWord.slice(i + 1);
33+
if (wordSet.has(nextWord) && !visited.has(nextWord)) {
34+
visited.add(nextWord);
35+
queue.push([nextWord, [...path, nextWord]]);
36+
}
37+
}
38+
}
39+
}
40+
41+
return [];
42+
}
43+
```
44+
45+
### Example Usage
46+
47+
```javascript
48+
import wordLadder from './wordLadder.js';
49+
50+
const beginWord = "hit";
51+
const endWord = "cog";
52+
const wordList = ["hot", "dot", "dog", "lot", "log", "cog"];
53+
const path = wordLadder(beginWord, endWord, wordList);
54+
console.log("Shortest transformation sequence:", path);
55+
```
56+
57+
---
58+
59+
## Versión en Español
60+
61+
### Descripción del Reto
62+
Dadas dos palabras (inicio y fin) y un diccionario, implementa un algoritmo para encontrar la secuencia de transformación más corta donde cada palabra difiere en exactamente un carácter de la anterior. Este reto requiere aplicar técnicas de búsqueda como BFS.
63+
64+
### Explicación del Código
65+
La solución usa un enfoque de búsqueda en anchura (BFS) para explorar todas las posibles transformaciones de un carácter desde la palabra actual. Usa una cola para llevar el seguimiento de la palabra actual y el camino tomado para llegar a ella. La búsqueda continúa hasta encontrar la palabra final o agotar todas las posibilidades.
66+
67+
### Fragmento de Código Relevante
68+
69+
```javascript
70+
function wordLadder(beginWord, endWord, wordList) {
71+
const wordSet = new Set(wordList);
72+
if (!wordSet.has(endWord)) {
73+
return [];
74+
}
75+
76+
const queue = [[beginWord, [beginWord]]];
77+
const visited = new Set([beginWord]);
78+
79+
while (queue.length > 0) {
80+
const [currentWord, path] = queue.shift();
81+
if (currentWord === endWord) {
82+
return path;
83+
}
84+
85+
for (let i = 0; i < currentWord.length; i++) {
86+
for (let c = 97; c <= 122; c++) { // 'a' to 'z'
87+
const char = String.fromCharCode(c);
88+
const nextWord = currentWord.slice(0, i) + char + currentWord.slice(i + 1);
89+
if (wordSet.has(nextWord) && !visited.has(nextWord)) {
90+
visited.add(nextWord);
91+
queue.push([nextWord, [...path, nextWord]]);
92+
}
93+
}
94+
}
95+
}
96+
97+
return [];
98+
}
99+
```
100+
101+
### Ejemplo de Uso
102+
103+
```javascript
104+
import wordLadder from './wordLadder.js';
105+
106+
const beginWord = "hit";
107+
const endWord = "cog";
108+
const wordList = ["hot", "dot", "dog", "lot", "log", "cog"];
109+
const path = wordLadder(beginWord, endWord, wordList);
110+
console.log("Secuencia de transformación más corta:", path);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// main.js - Example usage of wordLadder function
2+
3+
import wordLadder from './wordLadder.js';
4+
5+
/*
6+
Challenge: Given two words (start and end) and a dictionary, implement an algorithm to find the shortest transformation sequence where each word differs by exactly one character from the previous one. This challenge requires applying search techniques like BFS.
7+
*/
8+
9+
function main() {
10+
const beginWord = "hit";
11+
const endWord = "cog";
12+
const wordList = ["hot", "dot", "dog", "lot", "log", "cog"];
13+
const path = wordLadder(beginWord, endWord, wordList);
14+
console.log("Shortest transformation sequence:", path);
15+
}
16+
17+
main();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// wordLadder.js - Find shortest transformation sequence using BFS
2+
3+
function wordLadder(beginWord, endWord, wordList) {
4+
const wordSet = new Set(wordList);
5+
if (!wordSet.has(endWord)) {
6+
return [];
7+
}
8+
9+
const queue = [[beginWord, [beginWord]]];
10+
const visited = new Set([beginWord]);
11+
12+
while (queue.length > 0) {
13+
const [currentWord, path] = queue.shift();
14+
if (currentWord === endWord) {
15+
return path;
16+
}
17+
18+
for (let i = 0; i < currentWord.length; i++) {
19+
for (let c = 97; c <= 122; c++) { // 'a' to 'z'
20+
const char = String.fromCharCode(c);
21+
const nextWord = currentWord.slice(0, i) + char + currentWord.slice(i + 1);
22+
if (wordSet.has(nextWord) && !visited.has(nextWord)) {
23+
visited.add(nextWord);
24+
queue.push([nextWord, [...path, nextWord]]);
25+
}
26+
}
27+
}
28+
}
29+
30+
return [];
31+
}
32+
33+
export default wordLadder;

0 commit comments

Comments
 (0)