We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 508855e commit 71f413dCopy full SHA for 71f413d
Word_Break.cc
@@ -0,0 +1,18 @@
1
+class Solution {
2
+public:
3
+ bool wordBreak(string s, unordered_set<string> &dict) {
4
+ // Note: The Solution object is instantiated only once and is reused by each test case.
5
+ if (s == "")
6
+ return true;
7
+ vector<bool> dp(s.length() + 1, false);
8
+ dp[0] = true;
9
+ for (int i = 0; i < s.length(); i++) {
10
+ if (dp[i] == false)
11
+ continue;
12
+ for (int j = 1; i + j <= s.length(); j++)
13
+ if (dict.find(s.substr(i, j)) != dict.end())
14
+ dp[i + j] = true;
15
+ }
16
+ return dp[s.length()];
17
18
+};
0 commit comments