|
| 1 | +# Word Break |
| 2 | + |
| 3 | +## English |
| 4 | + |
| 5 | +The word break problem is a classic example of recursion with memoization, commonly used in natural language processing and text segmentation tasks. It efficiently explores multiple segmentation paths while avoiding redundant computations. |
| 6 | + |
| 7 | +This challenge involves returning all possible valid sentences that can be formed from a string using words from a dictionary. |
| 8 | + |
| 9 | +### Relevant Code Snippet |
| 10 | + |
| 11 | +```javascript |
| 12 | +class WordBreak { |
| 13 | + constructor(s, wordDict) { |
| 14 | + this.s = s; |
| 15 | + this.wordDict = new Set(wordDict); |
| 16 | + this.memo = new Map(); |
| 17 | + } |
| 18 | + |
| 19 | + wordBreak(start = 0) { |
| 20 | + if (start === this.s.length) { |
| 21 | + return [""]; |
| 22 | + } |
| 23 | + |
| 24 | + if (this.memo.has(start)) { |
| 25 | + return this.memo.get(start); |
| 26 | + } |
| 27 | + |
| 28 | + const sentences = []; |
| 29 | + for (let end = start + 1; end <= this.s.length; end++) { |
| 30 | + const word = this.s.substring(start, end); |
| 31 | + if (this.wordDict.has(word)) { |
| 32 | + const subsentences = this.wordBreak(end); |
| 33 | + for (const subsentence of subsentences) { |
| 34 | + const sentence = word + (subsentence ? " " + subsentence : ""); |
| 35 | + sentences.push(sentence); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + this.memo.set(start, sentences); |
| 41 | + return sentences; |
| 42 | + } |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +### History |
| 47 | + |
| 48 | +The word break problem has been widely studied in computer science and is commonly used in natural language processing and text segmentation. |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +## Español |
| 53 | + |
| 54 | +Segmentación de Palabras |
| 55 | + |
| 56 | +El problema de segmentación de palabras es un ejemplo clásico de recursión con memoización, comúnmente usado en procesamiento de lenguaje natural y tareas de segmentación de texto. Explora eficientemente múltiples caminos de segmentación evitando cálculos redundantes. |
| 57 | + |
| 58 | +Este reto consiste en devolver todas las posibles oraciones válidas que se pueden formar a partir de una cadena usando palabras de un diccionario. |
| 59 | + |
| 60 | +### Fragmento de Código Relevante |
| 61 | + |
| 62 | +```javascript |
| 63 | +class WordBreak { |
| 64 | + constructor(s, wordDict) { |
| 65 | + this.s = s; |
| 66 | + this.wordDict = new Set(wordDict); |
| 67 | + this.memo = new Map(); |
| 68 | + } |
| 69 | + |
| 70 | + wordBreak(start = 0) { |
| 71 | + if (start === this.s.length) { |
| 72 | + return [""]; |
| 73 | + } |
| 74 | + |
| 75 | + if (this.memo.has(start)) { |
| 76 | + return this.memo.get(start); |
| 77 | + } |
| 78 | + |
| 79 | + const sentences = []; |
| 80 | + for (let end = start + 1; end <= this.s.length; end++) { |
| 81 | + const word = this.s.substring(start, end); |
| 82 | + if (this.wordDict.has(word)) { |
| 83 | + const subsentences = this.wordBreak(end); |
| 84 | + for (const subsentence of subsentences) { |
| 85 | + const sentence = word + (subsentence ? " " + subsentence : ""); |
| 86 | + sentences.push(sentence); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + this.memo.set(start, sentences); |
| 92 | + return sentences; |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### Historia |
| 98 | + |
| 99 | +El problema de segmentación de palabras ha sido ampliamente estudiado en ciencias de la computación y es comúnmente usado en procesamiento de lenguaje natural y segmentación de texto. |
0 commit comments