[leetcode] Word Break II
来源:互联网 发布:php引号 编辑:程序博客网 时间:2024/05/18 02:29
https://leetcode.com/problems/word-break-ii/
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.
Return all such possible sentences.
For example, given
s = "catsanddog"
,
dict = ["cat", "cats", "and", "sand", "dog"]
.
A solution is ["cats and dog", "cat sand dog"]
.
public class Solution { private String s; private Set<String> dict; private int len; private LinkedList<String> wds; private List<String> sentences; private int[][] idxes;// idxes[i][j] means i can reach j boolean hasAns = false; public List<String> wordBreak(String s, Set<String> wordDict) { sentences = new ArrayList<String>(); if(null == s || "".equals(s)) { return sentences; } init(s, wordDict); checkReachable0(); if(hasAns) { find0(); } return sentences; } private void find0() { for(int i=0, n=idxes[0][len]; i<=n; ++i) { int k = idxes[0][i]+1; wds.add(s.substring(0, k)); findRest(k); wds.removeLast(); }}private void findRest(int st) {if(st == len) {addSentence();return;}for(int i=0, n=idxes[st][len]; i<=n; ++i) {int k = idxes[st][i] + 1;wds.add(s.substring(st, k)); findRest(k); wds.removeLast();}}private void checkReachable0() { for(int i=0; i<len; ++i) { if(dict.contains(s.substring(0, i+1))) { idxes[0][++idxes[0][len]] = i; hasAns |= (i+1 == len); checkReachable(i+1); } }}private void checkReachable(int st) {if(st < len && 0 > idxes[st][len]) {for(int i=st; i<len; ++i) {if(dict.contains(s.substring(st, i+1))) {idxes[st][++idxes[st][len]] = i; hasAns |= (i+1 == len); checkReachable(i+1);}}}} private void addSentence() {Iterator<String> it= wds.iterator();StringBuilder sb = new StringBuilder();if(it.hasNext()) {sb.append(it.next());}while(it.hasNext()) {sb.append(" ").append(it.next());}sentences.add(sb.toString());}private void init(String s, Set<String> wordDict) { this.len = s.length(); this.s = s; this.dict = wordDict; this.wds = new LinkedList<String>(); this.idxes = new int[len][len+1]; for(int i=0; i<len; ++i) { // -1 means this idx can reach nowhere idxes[i][len] = -1; } }}
public class Solution {private String s;private Set<String> dict;private int len;private LinkedList<String> wds;private List<String> sentences;// idxes[i][k]=j: means that i can reach j,k is within[0, count[i]]private int[][] idxes;// count[i]: the number of word which starts at i and exits in wordDictprivate int[] count;boolean hasAns = false;public List<String> wordBreak(String s, Set<String> wordDict) {sentences = new ArrayList<String>();if (null == s || "".equals(s)) {return sentences;}init(s, wordDict);checkReachable();if (hasAns) {find(0);}return sentences;}private void find(int st) {if (st == len) {addSentence();return;}for (int i = 0; i < count[st]; ++i) {int k = idxes[st][i] + 1;wds.add(s.substring(st, k));find(k);wds.removeLast();}}/** * 从0开始找,找到所有可能的路径. */private void checkReachable() {for (int i = 0; i < len; ++i) {if (dict.contains(s.substring(0, i + 1))) {idxes[0][count[0]++] = i;hasAns |= (i + 1 == len);checkReachableRest(i + 1);}}}private void checkReachableRest(int st) {if (st < len && 0 == count[st]) {// 遍历过一次的,就全部找到了;所以要有這個條件開著,否則會多次遍歷。for (int i = st; i < len; ++i) {if (dict.contains(s.substring(st, i + 1))) {idxes[st][count[st]++] = i;hasAns |= (i + 1 == len);checkReachableRest(i + 1);}}}}private void addSentence() {Iterator<String> it = wds.iterator();StringBuilder sb = new StringBuilder();if (it.hasNext()) {sb.append(it.next());}while (it.hasNext()) {sb.append(" ").append(it.next());}sentences.add(sb.toString());}private void init(String s, Set<String> wordDict) {this.len = s.length();this.s = s;this.dict = wordDict;this.wds = new LinkedList<String>();this.idxes = new int[len][len];this.count = new int[len];}}
public class Solution { private String s;private Set<String> dict;private int len;private LinkedList<String> wds;private List<String> sentences;// idxes[i][k]=j: means that i can reach j,k is within[0, count[i]]private int[][] idxes;// count[i]: the number of word which starts at i and exits in wordDictprivate int[] count;boolean hasAns = false;public List<String> wordBreak(String s, Set<String> wordDict) {sentences = new ArrayList<String>();if (null == s || "".equals(s)) {return sentences;}init(s, wordDict);checkReachable();if (hasAns) {find(0);}return sentences;}private void find(int st) {if (st == len) {addSentence();return;}for (int i = 0; i < count[st]; ++i) {int k = idxes[st][i] + 1;wds.add(s.substring(st, k));find(k);wds.removeLast();}}/** * 从0开始找,找到所有可能的路径. */private void checkReachable() {Queue<Integer> q = new LinkedList<Integer>();q.add(0);while (!q.isEmpty()) {int st = q.poll();hasAns |= (st == len);if (st < len && count[st] == 0) {// 没有计算过st的countfor (int i = st; i < len; ++i) {if (dict.contains(s.substring(st, i + 1))) {q.add(i + 1);idxes[st][count[st]++] = i;}}}}}private void addSentence() {Iterator<String> it = wds.iterator();StringBuilder sb = new StringBuilder();if (it.hasNext()) {sb.append(it.next());}while (it.hasNext()) {sb.append(" ").append(it.next());}sentences.add(sb.toString());}private void init(String s, Set<String> wordDict) {this.len = s.length();this.s = s;this.dict = wordDict;this.wds = new LinkedList<String>();this.idxes = new int[len][len];this.count = new int[len];}}
0 0
- [leetcode]Word Break II
- LeetCode:Word Break II
- Leetcode: Word Break II
- [LeetCode] Word Break II
- [LeetCode]Word Break II
- LeetCode | Word Break II
- [LeetCode] - Word Break II
- LeetCode - Word Break II
- Leetcode Word Break II
- [LeetCode] Word Break II
- Word Break II -- LeetCode
- LeetCode (Word Break II )
- Leetcode--Word Break II
- Leetcode: Word Break II
- LeetCode Word Break II
- Word Break II leetcode
- [leetcode]Word Break II
- leetcode -- word break II
- aauto学习系列之<6>控制语句1
- easyui赋值问题
- visual studio与visual c++ 6.0的区别
- servlet请求转发、包含以及重定向
- LintCode-数组划分
- [leetcode] Word Break II
- Nginx简介
- 理解Windows中的路由表和默认网关
- 第三章 控制语句
- Java的内存机制
- 自定义链接提示
- O'REILLY系列图书排版要求:
- Eclipse导入web项目变为java项目的解决办法
- 平衡查找树之红黑树