TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
word_break.cpp
Go to the documentation of this file.
1
30
31
#include <cassert>
32
#include <climits>
33
#include <iostream>
34
#include <string>
35
#include <unordered_set>
36
#include <vector>
37
42
namespace
dynamic_programming
{
43
49
namespace
word_break
{
50
60
bool
exists
(
const
std::string &str,
61
const
std::unordered_set<std::string> &strSet) {
62
return
strSet.find(str) != strSet.end();
63
}
64
80
bool
check
(
const
std::string &s,
const
std::unordered_set<std::string> &strSet,
81
int
pos, std::vector<int> *
dp
) {
82
if
(pos == s.length()) {
83
// if we have reached till the end of the string, means we have
84
// segmented throughout correctly hence we have a solution, thus
85
// returning true
86
return
true
;
87
}
88
89
if
(
dp
->at(pos) != INT_MAX) {
90
// if dp[pos] is not INT_MAX, means we must have saved a solution
91
// for the position pos; then return if the solution at pos is true
92
// or not
93
return
dp
->at(pos) == 1;
94
}
95
96
std::string wordTillNow =
97
""
;
// string to save the prefixes of word till different positons
98
99
for
(
int
i = pos; i < s.length(); i++) {
100
// Loop starting from pos to end, to check valid set of
101
// segmentations if any
102
wordTillNow +=
103
std::string(1, s[i]);
// storing the prefix till the position i
104
105
// if the prefix till current position is present in the dictionary
106
// and the remaining substring can also be segmented legally, then
107
// set solution at position pos in the memo, and return true
108
if
(
exists
(wordTillNow, strSet) &&
check
(s, strSet, i + 1,
dp
)) {
109
dp
->at(pos) = 1;
110
return
true
;
111
}
112
}
113
// if function has still not returned, then there must be no legal
114
// segmentation possible after segmenting at pos
115
dp
->at(pos) = 0;
// so set solution at pos as false
116
return
false
;
// and return no solution at position pos
117
}
118
131
bool
wordBreak
(
const
std::string &s,
const
std::vector<std::string> &wordDict) {
132
// unordered set to store words in the dictionary for constant time
133
// search
134
std::unordered_set<std::string> strSet;
135
for
(
const
auto
&s : wordDict) {
136
strSet.insert(s);
137
}
138
// a vector to be used for memoization, whose value at index i will
139
// tell if the string s can be segmented (correctly) at position i.
140
// initializing it with INT_MAX (which will denote no solution)
141
std::vector<int>
dp
(s.length(), INT_MAX);
142
143
// calling check method with position = 0, to check from left
144
// from where can be start segmenting the complete string in correct
145
// manner
146
return
check
(s, strSet, 0, &
dp
);
147
}
148
149
}
// namespace word_break
150
}
// namespace dynamic_programming
151
156
static
void
test
() {
157
// the complete string
158
const
std::string s =
"applepenapple"
;
159
// the dictionary to be used
160
const
std::vector<std::string> wordDict = {
"apple"
,
"pen"
};
161
162
assert(
dynamic_programming::word_break::wordBreak
(s, wordDict));
163
164
// should return true, as applepenapple can be segmented as apple + pen +
165
// apple
166
std::cout <<
dynamic_programming::word_break::wordBreak
(s, wordDict)
167
<< std::endl;
168
std::cout <<
"Test implementation passed!\n"
;
169
}
170
174
int
main
() {
175
test
();
// call the test function :)
176
177
// the complete string
178
const
std::string s =
"applepenapple"
;
179
// the dictionary to be used
180
const
std::vector<std::string> wordDict = {
"apple"
,
"pen"
};
181
182
// should return true, as applepenapple can be segmented as apple + pen +
183
// apple
184
std::cout <<
dynamic_programming::word_break::wordBreak
(s, wordDict)
185
<< std::endl;
186
}
test
void test()
Definition
caesar_cipher.cpp:100
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
dp
for std::vector
Definition
partition_problem.cpp:39
dynamic_programming
Dynamic Programming algorithms.
word_break
Functions for Word Break problem.
dynamic_programming::word_break::exists
bool exists(const std::string &str, const std::unordered_set< std::string > &strSet)
Function that checks if the string passed in param is present in the the unordered_set passed.
Definition
word_break.cpp:60
dynamic_programming::word_break::check
bool check(const std::string &s, const std::unordered_set< std::string > &strSet, int pos, std::vector< int > *dp)
Function that checks if the string passed in param can be segmented from position 'pos',...
Definition
word_break.cpp:80
dynamic_programming::word_break::wordBreak
bool wordBreak(const std::string &s, const std::vector< std::string > &wordDict)
Function that checks if the string passed in param can be segmented into the strings present in the v...
Definition
word_break.cpp:131
dynamic_programming
word_break.cpp
Generated by
1.18.0