|
| 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); |
0 commit comments