-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path139.word-break.cpp
More file actions
111 lines (105 loc) · 2.93 KB
/
139.word-break.cpp
File metadata and controls
111 lines (105 loc) · 2.93 KB
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
/*
* @lc app=leetcode id=139 lang=cpp
*
* [139] Word Break
*
* https://leetcode.com/problems/word-break/description/
*
* algorithms
* Medium (33.04%)
* Likes: 3025
* Dislikes: 166
* Total Accepted: 423.8K
* Total Submissions: 1.1M
* Testcase Example: '"leetcode"\n["leet","code"]'
*
* Given a non-empty string s and a dictionary word_dict containing a list of
* non-empty words, determine if s can be segmented into a space-separated
* sequence of one or more dictionary words.
*
* Note:
*
*
* The same word in the dictionary may be reused multiple times in the
* segmentation.
* You may assume the dictionary does not contain duplicate words.
*
*
* Example 1:
*
*
* Input: s = "leetcode", word_dict = ["leet", "code"]
* Output: true
* Explanation: Return true because "leetcode" can be segmented as "leet
* code".
*
*
* Example 2:
*
*
* Input: s = "applepenapple", word_dict = ["apple", "pen"]
* Output: true
* Explanation: Return true because "applepenapple" can be segmented as "apple
* pen apple".
* Note that you are allowed to reuse a dictionary word.
*
*
* Example 3:
*
*
* Input: s = "catsandog", word_dict = ["cats", "dog", "sand", "and", "cat"]
* Output: false
*
*
*/
// @lc code=start
class Solution {
public:
// state of a given index in the string
// EITHER an index is unvisited i.e. no other strings from that index have
// been considered as words
// OR it has been visited and possible words have been found in the
// dictionary from that index forward
// OR it has been visited and no words in the dictionary have been found from
// that index
enum state { UNVISITED, FOUND, NOT_FOUND };
bool wordExists(const string& word, const vector<string>& word_dict) {
for (string s : word_dict) {
if (word == s) {
return true;
}
}
return false;
}
bool wordBreakHelper(string s, int start_index, vector<string>& word_dict,
vector<state>& memo) {
if (start_index == s.size()) {
return true;
}
// this start_index has been visited before and no words have been found
// in the dictionary from this index forward
if (memo[start_index] == NOT_FOUND) {
return false;
}
for (int end_index = start_index; end_index < s.size(); ++end_index) {
string prefix = s.substr(start_index, end_index - start_index + 1);
if (wordExists(prefix, word_dict)) {
memo[start_index] = FOUND;
bool matched_till_end =
wordBreakHelper(s, end_index + 1, word_dict, memo);
if (matched_till_end) {
return true;
}
}
}
// if you get till this point then no words in the dictionary have been
// found from that index forward
memo[start_index] = NOT_FOUND;
return false;
}
bool wordBreak(string s, vector<string>& word_dict) {
vector<state> memo(s.size(), UNVISITED);
return wordBreakHelper(s, 0, word_dict, memo);
}
};
// @lc code=end