diff --git a/dynamic_programming/word_break.cpp b/dynamic_programming/word_break.cpp index 6d6d9ec0c..2806d5763 100644 --- a/dynamic_programming/word_break.cpp +++ b/dynamic_programming/word_break.cpp @@ -24,26 +24,26 @@ Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] Output: false */ +#include + #include -#include -#include -#include -#include -#include #include -#include #include #include -using namespace std; +using std::cout; +using std::endl; +using std::string; +using std::unordered_set; +using std::vector; class Solution { public: - bool exists(string s, unordered_set strSet) { + bool exists(const string s, const unordered_set strSet) { return strSet.find(s) != strSet.end(); } - bool check(string s, unordered_set strSet, int pos, + bool check(const string s, const unordered_set strSet, int pos, vector& dp) { if (pos == s.length()) { return true; @@ -65,9 +65,9 @@ class Solution { return false; } - bool wordBreak(string s, vector& wordDict) { + bool wordBreak(const string s, const vector& wordDict) { unordered_set strSet; - for (auto s : wordDict) { + for (const auto s : wordDict) { strSet.insert(s); } @@ -75,3 +75,9 @@ class Solution { return check(s, strSet, 0, dp); } }; + +int main() { + const string s = "applepenapple"; + const vector wordDict = {"apple", "pen"}; + cout << Solution().wordBreak(s, wordDict) << endl; +} \ No newline at end of file