-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWord_Breaker.cpp
More file actions
43 lines (38 loc) · 903 Bytes
/
Copy pathWord_Breaker.cpp
File metadata and controls
43 lines (38 loc) · 903 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <string>
#include <vector>
#include <boost/unordered_set>
using namespace std;
vector<string> wordBreak(string s, unordered_set<string>& wordDict)
{
std::vector<string> res;
res.clear();
if(s.length() == 0) return res;
helper(s, wordDict, "", res, 0);
return res;
}
void helper(string s, unordered_set<string>& dict, string item, vector<string>& res, int start )
{
if(start >= s.length())
{
res.push_back(item);
return;
}
for (int i = start; i < s.length(); ++i)
{
string substr1 = s.substr(start, i);
if (dict.find(substr1) != dict.end())
{
item = (item.length() == 0) ? (substr1) : (item + " " + substr1);
helper(s, dict, item, res, i);
}
}
}
int int main(int argc, char const *argv[])
{
/* code */
string s = "catsanddog";
unordered_set<string> dict = {"cat", "cats", "and", "sand", "dog"};
wordBreak(s, dict);
return 0;
}