Skip to content

Commit d2eb296

Browse files
Merge pull request #434 from rajethanm5/main
word_break.cpp
2 parents df6b907 + 73344f5 commit d2eb296

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Dynamic Programming/word_break.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
using namespace std;
5+
6+
void wordBreak(vector<string> const &dict, string word, string out)
7+
{
8+
if (word.size() == 0)
9+
{
10+
cout << out << endl;
11+
return;
12+
}
13+
14+
for (int i = 1; i <= word.size(); i++)
15+
{
16+
17+
string prefix = word.substr(0, i);
18+
19+
20+
if (find(dict.begin(), dict.end(), prefix) != dict.end()) {
21+
wordBreak(dict, word.substr(i), out + " " + prefix);
22+
}
23+
}
24+
}
25+
26+
int main()
27+
{
28+
29+
vector<string> dict = { "this", "th", "is", "famous", "Word", "break",
30+
"b", "r", "e", "a", "k", "br", "bre", "brea", "ak", "problem" };
31+
32+
string word = "Wordbreakproblem";
33+
34+
wordBreak(dict, word, "");
35+
36+
return 0;
37+
}

0 commit comments

Comments
 (0)