139. Word Break(dp)
2016-08-18 23:27
155 查看
题目:
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 =
dict =
Return true because
查找词典里面的字符串能否组成字符串s。
结题思路:
状态转移方程:
dp[i]=dp[j]&&s(j~i) in dict (0<j<i)
dp[i]表示前i个字符能由词典的词构成。
代码:
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".
查找词典里面的字符串能否组成字符串s。
结题思路:
状态转移方程:
dp[i]=dp[j]&&s(j~i) in dict (0<j<i)
dp[i]表示前i个字符能由词典的词构成。
代码:
public class Solution { public boolean wordBreak(String s, Set<String> wordDict) { boolean dp[] = new boolean[s.length()+1]; dp[0]=true; for(int i=0;i<s.length();i++) { if(dp[i]) { for(int len = 1;len<s.length()+1-i;len++) { if(wordDict.contains(s.substring(i, i+len))) { dp[i+len]=true; } } } } return dp[s.length()]; } }
相关文章推荐
- Leetcode 139. Word Break | DP
- 139. Word Break (String; DP)
- leetcode 139. Word Break(dp,字典匹配)
- 【Leetcode】139. Word Break
- 139. Word Break
- 139. Word Break
- leetcode139 Word Break
- leetcode——139——Word Break
- 139. Word Break
- LeetCode(139) Word Break
- 139. Word Break
- 【Medium】139. Word Break
- LeetCode 139 Word Break (DFS 分词 解存在性判断)
- [leetcode] 139.Word Break
- 139. Word Break
- 139. Word Break
- 139. Word Break
- 第十一周:[Leetcode]139. Word Break
- Word Break,递归,暂存中间结果,“土地换和平”,dp
- [LeetCode]139 Word Break