forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0140.cpp
35 lines (31 loc) · 1.01 KB
/
0140.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
#include <vector>
#include <string>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
vector<string> wordBreak(string s, vector<string>& wordDict)
{
vector<string> out = _wordBreak(0, s, wordDict);
for (int i = 0; i < out.size(); ++i) out[i].pop_back();
return out;
}
private:
unordered_map<int, vector<string> > mem;
vector<string> _wordBreak(int st, string& s, vector<string>& wordDict)
{
if (mem.count(st)) return mem[st];
for (int i = 0; i < wordDict.size(); ++i)
{
if (s.substr(st, wordDict[i].length()) == wordDict[i])
{
vector<string> sublist = _wordBreak(st + wordDict[i].length(), s, wordDict);
for(auto& it : sublist)
mem[st].push_back(wordDict[i] + " " +it);
}
}
if (!mem.count(st) && st == s.length()) mem[st].push_back("");
return mem[st];
}
};