-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWord_Break.cpp
More file actions
55 lines (46 loc) · 1.24 KB
/
Word_Break.cpp
File metadata and controls
55 lines (46 loc) · 1.24 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
49
50
51
52
53
54
55
// Source : https://oj.leetcode.com/problems/word-break/
// Author : zheng yi xiong
// Date : 2014-11-30
/**********************************************************************************
*
* Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
* For example, given
* s = "leetcode",
* dict = ["leet", "code"].
* Return true because "leetcode" can be segmented as "leet code".
*
**********************************************************************************/
#include "stdafx.h"
#include <string>
#include <vector>
using namespace std;
class CWord_Break {
public:
bool wordBreak(string s, unordered_set<string> &dict) {
if (s.empty())
{
return false;
}
vector<int> vec_end_pos;
int len = s.size();
int end_pos = 1;
vec_end_pos.push_back(0);
for (; end_pos <= len; ++end_pos)
{
for (vector<int>::reverse_iterator it = vec_end_pos.rbegin(); it != vec_end_pos.rend(); ++it)
{
string sub_str = s.substr((*it), end_pos - (*it));
if (dict.find(sub_str) != dict.end())
{
vec_end_pos.push_back(end_pos);
break;
}
}
}
if (len == vec_end_pos.back())
{
return true;
}
return false;
}
};