Skip to content

Commit 82b887e

Browse files
authored
Merge pull request #3944 from kosuri-indu/add/word-break
Added: DP Algorithms - Word Break
2 parents 33163d4 + b4aa0e7 commit 82b887e

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
---
2+
id: word-break-problem
3+
title: Word Break Problem
4+
sidebar_label: Word Break Problem
5+
tags: [python, java, c++, programming, algorithms, dynamic programming, tutorial, in-depth]
6+
description: In this tutorial, we will learn about the Word Break Problem and its implementation in Python, Java, and C++ with detailed explanations and examples.
7+
---
8+
9+
# Word Break Problem
10+
11+
The Word Break Problem is a dynamic programming problem that involves determining if a given string can be segmented into a space-separated sequence of one or more dictionary words. It is commonly used in natural language processing and text analysis.
12+
13+
## Problem Statement
14+
15+
Given a string `s` and a dictionary of words `dict`, determine if `s` can be segmented into a space-separated sequence of one or more dictionary words.
16+
17+
## Example
18+
19+
For the string `s = "leetcode"` and dictionary `dict = ["leet", "code"]`, the output would be `True` because the string can be segmented into "leet code".
20+
21+
## Algorithm
22+
23+
1. Initialize a boolean array `dp` of length `n+1` where `n` is the length of the string `s`. The array `dp[i]` will be `True` if the substring `s[0:i]` can be segmented.
24+
2. Set `dp[0]` to `True` because an empty string can always be segmented.
25+
3. Iterate over the string `s` and update the `dp` array based on whether the substring ending at the current position can be segmented.
26+
27+
## Code for Word Break Problem
28+
29+
### Python Implementation
30+
31+
```python
32+
def word_break(s, word_dict):
33+
n = len(s)
34+
dp = [False] * (n + 1)
35+
dp[0] = True
36+
for i in range(1, n + 1):
37+
for j in range(i):
38+
if dp[j] and s[j:i] in word_dict:
39+
dp[i] = True
40+
break
41+
return dp[n]
42+
43+
s = "leetcode"
44+
word_dict = {"leet", "code"}
45+
print("Can the string be segmented:", word_break(s, word_dict))
46+
```
47+
48+
### Java Implementation
49+
50+
```java
51+
import java.util.*;
52+
public class WordBreak {
53+
public static boolean wordBreak(String s, Set<String> wordDict) {
54+
int n = s.length();
55+
boolean[] dp = new boolean[n + 1];
56+
dp[0] = true;
57+
for (int i = 1; i <= n; i++) {
58+
for (int j = 0; j < i; j++) {
59+
if (dp[j] && wordDict.contains(s.substring(j, i))) {
60+
dp[i] = true;
61+
break;
62+
}
63+
}
64+
}
65+
return dp[n];
66+
}
67+
68+
public static void main(String[] args) {
69+
String s = "leetcode";
70+
Set<String> wordDict = new HashSet<>(Arrays.asList("leet", "code"));
71+
System.out.println("Can the string be segmented: " + wordBreak(s, wordDict));
72+
}
73+
}
74+
```
75+
76+
### Cpp Implementation
77+
78+
```cpp
79+
#include <iostream>
80+
#include <vector>
81+
#include <unordered_set>
82+
#include <string>
83+
84+
using namespace std;
85+
86+
bool wordBreak(const string& s, const unordered_set<string>& wordDict) {
87+
int n = s.length();
88+
vector<bool> dp(n + 1, false);
89+
dp[0] = true;
90+
for (int i = 1; i <= n; i++) {
91+
for (int j = 0; j < i; j++) {
92+
if (dp[j] && wordDict.find(s.substr(j, i - j)) != wordDict.end()) {
93+
dp[i] = true;
94+
break;
95+
}
96+
}
97+
}
98+
return dp[n];
99+
}
100+
101+
int main() {
102+
string s = "leetcode";
103+
unordered_set<string> wordDict = {"leet", "code"};
104+
cout << "Can the string be segmented: " << (wordBreak(s, wordDict) ? "True" : "False") << endl;
105+
return 0;
106+
}
107+
```
108+
109+
## Output
110+
111+
`Can the string be segmented: True`
112+
113+
114+
## Time Complexity
115+
116+
The time complexity of the Word Break Problem is $O(n^2 * k)$ where n is the length of the string and k is the average length of the words in the dictionary. This is due to the nested loops and substring checks.
117+
118+
## Space Complexity
119+
120+
The space complexity is $O(n)$ for storing the boolean array dp.
121+
122+
## Conclusion
123+
124+
The Word Break Problem is a classic dynamic programming problem that demonstrates the power of dynamic programming in solving string segmentation and text analysis problems. This algorithm efficiently determines if a string can be segmented into dictionary words.

0 commit comments

Comments
 (0)