forked from Mohammed-Shoaib/Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC0140.cpp
More file actions
executable file
·48 lines (42 loc) · 1.22 KB
/
LC0140.cpp
File metadata and controls
executable file
·48 lines (42 loc) · 1.22 KB
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
42
43
44
45
46
47
48
/*
Problem Statement: https://leetcode.com/problems/word-break-ii/
Time: O(2ⁿ • max_len)
Space: O(2ⁿ + words)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class Solution {
public:
vector<string> wordBreak(string s, vector<string>& wordDict) {
int max_len, n = s.length();
vector<bool> dp(n + 1);
vector<string> sentences;
vector<vector<int>> adj(n);
unordered_set<string> words(wordDict.begin(), wordDict.end());
// initialization
dp[0] = true;
max_len = 0;
for (string& word: wordDict)
max_len = max(static_cast<int>(word.length()), max_len);
// dynamic programming
for (int i = 1; i <= n; i++)
for (int j = i - 1; j >= max(0, i - max_len); j--)
if (dp[j] && words.count(s.substr(j, i - j))) {
dp[i] = true;
adj[j].push_back(i);
}
// use backtracking to construct all possible sentences
if (dp[n])
dfs(0, s, "", sentences, adj);
return sentences;
}
void dfs(int u, string& s, string sentence, vector<string>& sentences, vector<vector<int>>& adj) {
if (u == adj.size()) {
sentences.push_back(sentence);
return;
}
if (!sentence.empty())
sentence += " ";
for (int v: adj[u])
dfs(v, s, sentence + s.substr(u, v - u), sentences, adj);
}
};