forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
41 lines (30 loc) · 942 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/// Source : https://leetcode.com/problems/word-break/
/// Author : liuyubobobo
/// Time : 2021-02-25
#include <iostream>
#include <vector>
using namespace std;
/// Memory Search
/// Time Complexity: O(|s| * |wordDict|)
/// Space Complexity: O(|s|)
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
vector<int> dp(s.size(), -1);
return dfs(s, 0, wordDict, dp);
}
private:
bool dfs(const string& s, int index, const vector<string>& wordDict,
vector<int>& dp){
if(index == s.size()) return true;
if(dp[index] != -1) return dp[index];
int left = s.size() - index;
for(const string& word: wordDict)
if(left >= word.size() && s.substr(index, word.size()) == word &&
dfs(s, index + word.size(), wordDict, dp)) return dp[index] = true;
return dp[index] = false;
}
};
int main() {
return 0;
}