-
Notifications
You must be signed in to change notification settings - Fork 0
/
word_break.cpp
111 lines (98 loc) · 2.65 KB
/
word_break.cpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// =====================================================================================
//
// Filename: word_break.cpp
//
// Description: 139. Word Break.
// Given a non-empty string s and a dictionary wordDict containing a
// list of non-empty words, determine if s can be segmented into a
// space-separated sequence of one or more dictionary words.
//
// Version: 1.0
// Created: 09/03/2019 06:40:20 PM
// Revision: none
// Compiler: g++
//
// Author: Zhu Xianfeng (), [email protected]
// Organization:
//
// =====================================================================================
#include <stdio.h>
#include <string>
#include <vector>
#include <unordered_set>
using namespace std;
// DFS, time limit exceeded
class Solution1
{
public:
bool wordBreak(string& s, vector<string>& word_dict)
{
unordered_set<string> word_set(word_dict.begin(), word_dict.end());
bool found = false;
dfsTraverse(s, word_set, 0, &found);
return found;
}
private:
void dfsTraverse(const string& s, const unordered_set<string>& word_set, int start, bool* found)
{
if (start == s.size())
{
*found = true;
return;
}
for (int i = start; i < s.size(); i++)
{
auto seg = s.substr(start, i - start + 1);
if (word_set.count(seg) > 0)
{
dfsTraverse(s, word_set, i + 1, found);
}
if (*found)
{
break;
}
}
}
};
// DP, accepted
class Solution2
{
public:
bool wordBreak(string& s, vector<string>& word_dict)
{
vector<bool> dp(s.size() + 1, false);
// Initial first as true
dp[0] = true;
for (int i = 0; i < s.size(); i++)
{
if (!dp[i])
{
// Can't split
continue;
}
for (const auto& word: word_dict)
{
int len = word.size();
if (i + len > s.size())
{
continue;
}
if (s.substr(i, len) != word)
{
continue;
}
dp[i + len] = true;
}
}
return dp.back();
}
};
using Solution = Solution2;
int main(int argc, char* argv[])
{
string s = "leetcode";
vector<string> word_dict = {"leet", "code"};
bool found = Solution().wordBreak(s, word_dict);
printf("Found? %s\n", (found ? "true" : "false"));
return 0;
}