TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
word_break.cpp
Go to the documentation of this file.
1
31#include <cassert>
32#include <climits>
33#include <iostream>
34#include <string>
35#include <unordered_set>
36#include <vector>
37
42namespace dynamic_programming {
43
49namespace word_break {
50
60bool exists(const std::string &str,
61 const std::unordered_set<std::string> &strSet) {
62 return strSet.find(str) != strSet.end();
63}
64
80bool 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
131bool 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
156static 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}
174int 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}
for std::vector
Dynamic Programming algorithms.
Functions for Word Break problem.
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.
static void test()
Test implementations.
int main()
Main function.
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...