A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/d3/d84/word__break_8cpp_source.html below:

TheAlgorithms/C++: dynamic_programming/word_break.cpp Source File

Go to the documentation of this file. 35#include <unordered_set> 61 const

std::unordered_set<std::string> &strSet) {

62 return

strSet.find(str) != strSet.end();

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()) {

89 if

(

dp

->at(pos) != INT_MAX) {

93 return dp

->at(pos) == 1;

96

std::string wordTillNow =

99 for

(

int

i = pos; i < s.length(); i++) {

103

std::string(1, s[i]);

108 if

(

exists

(wordTillNow, strSet) &&

check

(s, strSet, i + 1,

dp

)) {

131bool wordBreak

(

const

std::string &s,

const

std::vector<std::string> &wordDict) {

134

std::unordered_set<std::string> strSet;

135 for

(

const auto

&s : wordDict) {

141

std::vector<int>

dp

(s.length(), INT_MAX);

146 return check

(s, strSet, 0, &

dp

);

158 const

std::string s =

"applepenapple"

;

160 const

std::vector<std::string> wordDict = {

"apple"

,

"pen"

};

168

std::cout <<

"Test implementation passed!\n"

;

178 const

std::string s =

"applepenapple"

;

180 const

std::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