Word Break(Java)
来源:互联网 发布:鼎捷软件 听证会 编辑:程序博客网 时间:2024/05/20 21:24
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”.
- Naive Approach
This problem can be solve by using a naive approach, which is trivial. A discussion can always start from that though.
public class Solution {
public boolean wordBreak(String s, Set dict) {
return wordBreakHelper(s, dict, 0);
}
public boolean wordBreakHelper(String s, Set<String> dict, int start){ if(start == s.length()) return true; for(String a: dict){ int len = a.length(); int end = start+len; //end index should be <= string length if(end > s.length()) continue; if(s.substring(start, start+len).equals(a)) if(wordBreakHelper(s, dict, start+len)) return true; } return false;}
}
Time is O(n^2) and exceeds the time limit.
- Dynamic Programming
The key to solve this problem by using dynamic programming approach:
Define an array t[] such that t[i]==true => 0-(i-1) can be segmented using dictionary
Initial state t[0] == true
public class Solution {
public boolean wordBreak(String s, Set dict) {
boolean[] t = new boolean[s.length()+1];
t[0] = true; //set first to be true, why?
//Because we need initial state
for(int i=0; i<s.length(); i++){ //should continue from match position if(!t[i]) continue; for(String a: dict){ int len = a.length(); int end = i + len; if(end > s.length()) continue; if(t[end]) continue; if(s.substring(i, end).equals(a)){ t[end] = true; } } } return t[s.length()];}
}
Time: O(string length * dict size).
- Java Solution 3 - Simple and Efficient
In Solution 2, if the size of the dictionary is very large, the time is bad. Instead we can solve the problem in O(n^2) time (n is the length of the string).
public boolean wordBreak(String s, Set wordDict) {
int[] pos = new int[s.length()+1];
Arrays.fill(pos, -1);pos[0]=0;for(int i=0; i<s.length(); i++){ if(pos[i]!=-1){ for(int j=i+1; j<=s.length(); j++){ String sub = s.substring(i, j); if(wordDict.contains(sub)){ pos[j]=i; } } }}return pos[s.length()]!=-1;
}
转载地址:http://www.programcreek.com/2012/12/leetcode-solution-word-break/
- [Leetcode] Word Break (Java)
- [leetcode] word break, java
- Word Break II Java
- Word Break LeetCode Java
- 【Leetcode】Word Break JAVA
- Word Break (Java)
- [letecode Java] Word Break
- Word Break(Java)
- word-break Java code
- word-break Java code
- [Leetcode] Word Break II (Java)
- Java Word Break(单词拆解)
- [leetcode]139. Word Break(Java)
- Word-break:break-word
- Word Break && Word Break ||
- [leetcode-139]Word Break(java)
- LeetCode – Word Break II (Java)
- LeetCode-139. Word Break (JAVA)单词切分
- SDUT 3347 数据结构实验之数组三:快速转置
- Codeforce 490E(树形dp)
- 浅析人脸检测之Haar分类器方法:Haar特征、积分图、 AdaBoost 、级联
- linux14.04下caffe的安装步骤
- 使用ngrok的总结
- Word Break(Java)
- Ubuntu Server 16.04 安装MySQL并设置远程访问
- linux杂记6
- MFC调用com打开文件预览
- Java实现AVL的一系列操作
- c++ vector
- Java开发中的23种设计模式详解
- LeeCode No.113 Path Sum II
- noip斗地主