Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4352f44

Browse files
feat(js): add modular solution and documentation for hard challenge 10 - Word Break with recursion and memoization
1 parent 7f90fdd commit 4352f44

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// WordBreak.js - Word Break problem using recursion with memoization in JavaScript
2+
3+
class WordBreak {
4+
constructor(s, wordDict) {
5+
this.s = s;
6+
this.wordDict = new Set(wordDict);
7+
this.memo = new Map();
8+
}
9+
10+
wordBreak(start = 0) {
11+
if (start === this.s.length) {
12+
return [""];
13+
}
14+
15+
if (this.memo.has(start)) {
16+
return this.memo.get(start);
17+
}
18+
19+
const sentences = [];
20+
for (let end = start + 1; end <= this.s.length; end++) {
21+
const word = this.s.substring(start, end);
22+
if (this.wordDict.has(word)) {
23+
const subsentences = this.wordBreak(end);
24+
for (const subsentence of subsentences) {
25+
const sentence = word + (subsentence ? " " + subsentence : "");
26+
sentences.push(sentence);
27+
}
28+
}
29+
}
30+
31+
this.memo.set(start, sentences);
32+
return sentences;
33+
}
34+
}
35+
36+
export default WordBreak;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Challenge:
3+
Given a string and a dictionary, return all possible valid sentences that can be formed using recursion with memoization.
4+
5+
This solution follows DRY principles and is implemented in JavaScript.
6+
*/
7+
8+
import WordBreak from "./WordBreak.js";
9+
10+
function main() {
11+
const s = "catsanddog";
12+
const wordDict = ["cat", "cats", "and", "sand", "dog"];
13+
14+
const wordBreak = new WordBreak(s, wordDict);
15+
const sentences = wordBreak.wordBreak();
16+
17+
console.log("Possible sentences:");
18+
sentences.forEach((sentence) => console.log(sentence));
19+
}
20+
21+
main();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /