Skip to content

Commit

Permalink
Word break ii (#177)
Browse files Browse the repository at this point in the history
* add word break II

* impl word break II
  • Loading branch information
SKTT1Ryze authored Mar 4, 2024
1 parent 8f98279 commit 6f8208a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
83 changes: 83 additions & 0 deletions leetcode-cc/WordBreakII.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <numeric>
#include <unordered_set>
#include <utility>

#include "TestHelper.h"
#include "problem.h"
#include "solution.h"

using namespace std;

IMPLEMENT_PROBLEM_CLASS(
PWordBreakII, 140, DIFFI_HARD, TOPIC_ALGORITHMS, "Word Break II",
"Given a string s and a dictionary of strings wordDict, add spaces in s to "
"construct a sentence where each word is a valid dictionary word. Return "
"all such possible sentences in any order.",
{"Backtracking"});

class SWordBreakII : public ISolution {
public:
size_t problemId() const override { return 140; }
string name() const override {
return ("Solution for " + string("Word Break II"));
}
string location() const override { return __FILE_NAME__; }
int test() const override {
return testHelper<pair<string, vector<string>>, vector<string>>(
{
{"catsanddog", {"cat", "cats", "and", "sand", "dog"}},
{"pineapplepenapple",
{"apple", "pen", "applepen", "pine", "pineapple"}},

},
{{"cats and dog", "cat sand dog"},
{"pineapple pen apple", "pine applepen apple",
"pine apple pen apple"}},
[this](auto input) {
return this->wordBreak(get<0>(input), get<1>(input));
});
};
int benchmark() const override { return 0; }

private:
vector<string> wordBreak(string s, vector<string>& wordDict) const {
unordered_set<string> dict(wordDict.begin(), wordDict.end());
vector<string> current = {};
vector<string> paths = {};

this->backtracking(s, 0, 1, dict, current, paths);

return paths;
}

void backtracking(string s, int start, int end, unordered_set<string>& dict,
vector<string> current, vector<string>& paths) const {
int n = s.length();
if (start < n && end < n) {
string subs = s.substr(start, end - start);
if (dict.contains(subs)) {
this->backtracking(s, start, end + 1, dict, current, paths);
current.push_back(subs);
this->backtracking(s, end, end + 1, dict, current, paths);
} else {
this->backtracking(s, start, end + 1, dict, current, paths);
}
} else {
if (start < n) {
if (dict.contains(s.substr(start, n - start))) {
current.push_back(s.substr(start, n - start));
} else {
return;
}
}

if (!current.empty()) {
string path = accumulate(
std::next(std::begin(current)), std::end(current), current[0],
[](string ss, string s) { return ss + " " + s; });

paths.push_back(path);
}
}
}
};
6 changes: 6 additions & 0 deletions runtime-cc/src/registration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
#include "../leetcode-cc/ValidParentheses.hpp"
#include "../leetcode-cc/ValidateBST.hpp"
#include "../leetcode-cc/WordBreak.hpp"
#include "../leetcode-cc/WordBreakII.hpp"
#include "../leetcode-cc/WordLadder.hpp"
#include "../leetcode-cc/WordLadderII.hpp"

Expand Down Expand Up @@ -581,5 +582,10 @@ const int registerAll(std::shared_ptr<Container> handle) {
return std::make_shared<SCopyListWithRandPointer>();
});

handle->registerProblem(
[]() -> ArcProblem { return std::make_shared<PWordBreakII>(); });
handle->registerSolution(
[]() -> ArcSolution { return std::make_shared<SWordBreakII>(); });

return 0;
}

0 comments on commit 6f8208a

Please sign in to comment.