Skip to content

Commit 40d5967

Browse files
committed
Work Break solution
1 parent 7ed301a commit 40d5967

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

word-break/PDKhan.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
bool wordBreak(string s, vector<string>& wordDict) {
4+
vector<bool> dp(s.length()+1, false);
5+
6+
dp[0] = true;
7+
8+
for(int i = 1; i <= s.length(); i++){
9+
for(string& word : wordDict){
10+
int len = word.length();
11+
if(i - len >= 0 && s.substr(i-len, len) == word)
12+
dp[i] = dp[i - len];
13+
14+
if(dp[i])
15+
break;
16+
}
17+
}
18+
19+
return dp[s.length()];
20+
}
21+
};

0 commit comments

Comments
 (0)