Skip to content

Commit 71f413d

Browse files
committed
Create Word_Break.cc
1 parent 508855e commit 71f413d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Word_Break.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)