# 【LeetCode】139. Word Break 解题报告（Python & C++）

900 篇文章 155 订阅
1033 篇文章 56 订阅

id： fuxuemingzhu

## 题目描述

Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

Note:

• The same word in the dictionary may be reused multiple times in the segmentation.
• You may assume the dictionary does not contain duplicate words.

Example 1:

Input: s = "leetcode", wordDict = ["leet", "code"]
Output: true
Explanation: Return true because "leetcode" can be segmented as "leet code".


Example 2:

Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
Note that you are allowed to reuse a dictionary word.


Example 3:

Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: false


## 解题方法

1. 确定可以保存的信息
2. 递推式(以及如何在递推中使用保存的信息)
3. 确定起始条件

S能拆成功的话，说明

s[0:k]能拆成功，然后 s[k:i]是一个在字典中的单词。

"leetcode"
[u'leet', u'code']
[True, False, False, False, False, False, False, False, False]
[True, False, False, False, True, False, False, False, False]
[True, False, False, False, True, False, False, False, True]
"leetcode"
[u'le', u'et', u'code']
[True, False, False, False, False, False, False, False, False]
[True, False, True, False, False, False, False, False, False]
[True, False, True, False, True, False, False, False, False]
[True, False, True, False, True, False, False, False, True]
"leetcode"
[u'l', u'ee', u't', u'co', u'd', u'e']
[True, False, False, False, False, False, False, False, False]
[True, True, False, False, False, False, False, False, False]
[True, True, True, False, False, False, False, False, False]
[True, True, True, True, False, False, False, False, False]
[True, True, True, True, False, False, False, False, False]
[True, True, True, True, True, False, False, False, False]
[True, True, True, True, True, False, True, False, False]
[True, True, True, True, True, False, True, True, False]
[True, True, True, True, True, False, True, True, True]


Python代码：

class Solution(object):
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: List[str]
:rtype: bool
"""
print(s)
print(wordDict)
dp = [False] * (len(s) + 1)
dp[0] = True
print(dp)
for i in xrange(1, len(s) + 1):
for k in xrange(i):
if dp[k] and s[k:i] in wordDict:
dp[i] = True
print(dp)
return dp.pop()


class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
const int N = s.size();
unordered_set<string> wordSet(wordDict.begin(), wordDict.end());
// dp[i] means s[0:i) is wordBreak or not.
vector<bool> dp(N + 1, false);
dp[0] = true;
// i in range [0, N)
for (int i = 0; i <= N; ++i) {
for (int j = 0; j < i; ++j) {
if (dp[j] && wordSet.count(s.substr(j, i - j))) {
dp[i] = true;
break;
}
}
}
return dp.back();
}
};


## 日期

2018 年 2 月 25 日
2018 年 12 月 18 日 —— 改革开放40周年
2019 年 1 月 10 日 —— 加油

• 1
点赞
• 0
收藏
• 打赏
• 0
评论
01-05 77
09-05 1601
11-08 77

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

©️2022 CSDN 皮肤主题：猿与汪的秘密 设计师：我叫白小胖

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。