std::unordered_set<std::string> &strSet) {
62 returnstrSet.find(str) != strSet.end();
80bool check(
conststd::string &s,
conststd::unordered_set<std::string> &strSet,
81 intpos, std::vector<int> *
dp) {
82 if(pos == s.length()) {
89 if(
dp->at(pos) != INT_MAX) {
93 return dp->at(pos) == 1;
96std::string wordTillNow =
99 for(
inti = pos; i < s.length(); i++) {
103std::string(1, s[i]);
108 if(
exists(wordTillNow, strSet) &&
check(s, strSet, i + 1,
dp)) {
131bool wordBreak(
conststd::string &s,
conststd::vector<std::string> &wordDict) {
134std::unordered_set<std::string> strSet;
135 for(
const auto&s : wordDict) {
141std::vector<int>
dp(s.length(), INT_MAX);
146 return check(s, strSet, 0, &
dp);
158 conststd::string s =
"applepenapple";
160 conststd::vector<std::string> wordDict = {
"apple",
"pen"};
168std::cout <<
"Test implementation passed!\n";
178 conststd::string s =
"applepenapple";
180 conststd::vector<std::string> wordDict = {
"apple",
"pen"};
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.
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',...
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...
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4