-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path139.cpp
More file actions
35 lines (32 loc) · 829 Bytes
/
139.cpp
File metadata and controls
35 lines (32 loc) · 829 Bytes
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
//
// 139.cpp
// LeetCode
//
// Created by 张佐玮 on 15/7/14.
// Copyright (c) 2015年 JarvisZhang. All rights reserved.
//
// Title: Word Break
//
#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
using namespace std;
class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
string formattedS = "#" + s;
long length = formattedS.size();
vector<bool> matched(length, false);
matched[0] = true;
for(int i = 1; i < length; i++) {
for (int j = 0; j < i; j++) {
if (matched[j] && wordDict.find(formattedS.substr(j+1, i-j)) != wordDict.end()) {
matched[i] = true;
break;
}
}
}
return matched.back();
}
};