-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path139.cpp
36 lines (34 loc) · 879 Bytes
/
139.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
//
// 139.cpp
// leetcode
//
// Created by R Z on 2018/1/4.
// Copyright © 2018年 R Z. All rights reserved.
//
#include <stdio.h>
#include <vector>
#include <unordered_set>
#include <string>
using namespace std;
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
if(wordDict.size()==0) return false;
unordered_set<string> dict;
for(string str : wordDict) dict.insert(str);
vector<bool> dp(s.size()+1, false);
dp[0]=true;
for(int i=1; i<=s.size(); i++){
for(int j=i-1; j>=0; j--){
if(dp[j]){
string word = s.substr(j,i-j);
if(dict.find(word)!=dict.end()){
dp[i]=true;
break;
}
}
}
}
return dp[s.size()];
}
};