LeetCode 140. Word Break II(单词切分)
2016-05-27 00:22
483 查看
原题网址: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 =
dict =
A solution is
方法一:深度优先。
方法二:Trie+深度优先搜索。
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 ArrayList<String>[] scanned; private List<String> results = new ArrayList<>(); private void dfs(String s, int from, List<String> words, Set<String> wordDict) { if (from == s.length()) { String result = ""; for(int i=0; i<words.size(); i++) { if (i>0) result += " "; result += words.get(i); } results.add(result); return; } for(String word: scanned[from]) { words.add(word); dfs(s, from + word.length(), words, wordDict); words.remove(words.size()-1); } } public List<String> wordBreak(String s, Set<String> wordDict) { scanned = new ArrayList[s.length()]; scanned[0] = new ArrayList<>(); boolean reachable = false; for(int i=0; i<s.length(); i++) { if (scanned[i] == null) continue; for(String word: wordDict) { if (i + word.length() <= s.length() && word.equals(s.substring(i, i+word.length()))) { scanned[i].add(word); if (i+word.length() == s.length()) reachable = true; if (i+word.length() < s.length() && scanned[i+word.length()] == null) { scanned[i+word.length()] = new ArrayList<>(); } } } } if (!reachable) return results; dfs(s, 0, new ArrayList<>(), wordDict); return results; } }
方法二:Trie+深度优先搜索。
public class Solution { private TrieNode root; private void find(char[] sa, int from, int[] split, int splits, List<String> words) { if (from == sa.length) { char[] word = new char[sa.length+splits-1]; int wordPos = 0, splitPos = 0; for(int i=0; i<sa.length; i++) { if (i==split[splitPos]) { word[wordPos++] = ' '; splitPos++; } word[wordPos++] = sa[i]; } words.add(new String(word)); return; } TrieNode current = root; for(int i=from;i<sa.length;i++) { current = current.nexts[sa[i]-'a']; if (current == null) break; if (current.isWord) { split[splits] = i+1; find(sa, i+1, split, splits+1, words); } } } public List<String> wordBreak(String s, Set<String> wordDict) { List<String> results = new ArrayList<>(); if (s==null || wordDict==null) return results; root = new TrieNode(); for(String word: wordDict) { char[] wa = word.toCharArray(); TrieNode current = root; for(int i=0; i<wa.length; i++) current = current.add(wa[i]); current.isWord = true; } char[] sa = s.toCharArray(); boolean[] reachable = new boolean[sa.length+1]; reachable[0] = true; for(int i=0; i<sa.length; i++) { if (!reachable[i]) continue; TrieNode current = root; for(int j=i;j<sa.length && current != null;j++) { current = current.nexts[sa[j]-'a']; if (current != null && current.isWord) reachable[j+1] = true; } } if (!reachable[sa.length]) return results; find(sa, 0, new int[sa.length], 0, results); return results; } } class TrieNode { boolean isWord; TrieNode[] nexts = new TrieNode[26]; TrieNode add(char ch) { int i = ch - 'a'; if (nexts[i] != null) return nexts[i]; nexts[i] = new TrieNode(); return nexts[i]; } }
相关文章推荐
- LeetCode 139. Word Break(单词分隔)
- LeetCode 138. Copy List with Random Pointer(拷贝随机指针)
- LeetCode 137. Single Number II(单个数字)
- LeetCode 136. Single Number(单个数字)
- LeetCode 135. Candy(糖果)
- LeetCode 134. Gas Station(加油站)
- LeetCode 132. Palindrome Partitioning II(回文切分)
- LeetCode 133. Clone Graph(克隆图)
- 原型链(__proto__)
- makefile之VPATH和vpath的使用
- System.in.read()
- javascript的 Object 和 Function
- 快速多人游戏(1) - 介绍
- 概率世界
- 如何配置php运行环境
- Share Ubuntu Home Directories using Samba
- php性能测试工具--xhprof
- linux篇——centOs下的vim配置
- dll 子类化 记事本 相应自己的功能
- 用NSMutableAttributedString快速设置UIbutton Title/lable的文字:字体,大小,颜色