LeetCode: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"]
第一步:先用動態規劃找出所有的斷點,題目中的例子來說,在位置0,可以選擇cats或者cat,下一斷點為3和4。我的程式碼中buildPos()方法實現。
第二步:使用遞迴列出所有解。我的程式碼中的printResult()方法實現。
PS:我的方法命名太爛 了,看不下去了 。
package leetcode; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; public class WordBreakII { /** * @param args */ public static void main(String[] args) { String s = "catsanddog"; Set<String> dict = new HashSet<String>(); dict.add("cat"); dict.add("cats"); dict.add("and"); dict.add("sand"); dict.add("dog"); ArrayList<String> result = new WordBreakII().wordBreak(s, dict); for (String ss : result) { System.out.println(ss); } } private Map<Integer, List<Integer>> posMap = new HashMap<Integer, List<Integer>>(); private ArrayList<String> result = new ArrayList<String>(); public ArrayList<String> wordBreak(String s, Set<String> dict) { for (List<Integer> list : posMap.values()) { list.clear(); } result.clear(); if (dict == null || dict.isEmpty()) { return result; } buildPos(s, dict); StringBuffer sb = new StringBuffer(); printResult(s, 0, sb); return result; } void printResult(String s, int pos, StringBuffer sb) { if (pos == s.length()) { result.add(sb.toString()); return; } List<Integer> list = posMap.get(pos); for (Integer newPos : list) { if (pos != 0) { sb.append(' '); } sb.append(s.substring(pos, newPos)); printResult(s, newPos, sb); int appendLen = newPos - pos; if (pos != 0) { appendLen += 1; } sb.delete(sb.length() - appendLen, sb.length()); } } public void buildPos(String s, Set<String> dict) { List<Integer> list = new ArrayList<Integer>(); for (int i = s.length() - 1; i >= 0; i--) { int len = list.size(); for (int j = len - 1; j >= 0; j--) { int index = list.get(j); String sub = s.substring(i, index); if (dict.contains(sub)) { List<Integer> list2 = posMap.get(i); if (list2 == null) { list2 = new ArrayList<Integer>(); posMap.put(i, list2); } list2.add(index); } } String sub = s.substring(i); if (dict.contains(sub)) { List<Integer> list2 = posMap.get(i); if (list2 == null) { list2 = new ArrayList<Integer>(); posMap.put(i, list2); } list2.add(s.length()); } List<Integer> list2 = posMap.get(i); if (list2 != null && !list2.isEmpty()) { list.add(i); } } } }
相關推薦
LeetCode: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. Re
[LeetCode] 140. Word Break II java
contains clas emp dfs urn key 分析 超時 字典 題目: Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add
【leetcode】140. Word Break II
tco com inf strong 不能 bubuko 動態 leet 技術分享 題目如下: 解題思路:巨坑的一個題目。一般來說,leetcode這一類題目分為兩種形式,一種是問能不能分割/有幾種分割的方法,一種是列出所有分割的集合。本題是第二種,但是有一些特別坑的用例
LeetCode 140 Word Break II
[LeetCode 140. Word Break II](https://leetcode.com/problems/word-break-ii/description/) 在上一道的動態規劃的基礎上,使用DFS列印路徑。 在DFS的過程,需要記錄下路徑,這樣就不會超時了。
word-break-ii leetcode C++
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. Retu
python leetcode 139. Word Break 140. Word Break II
139. Word Break class Solution: def wordBreak(self, s, wordDict): """ :type s: str :type wordDict: List[str]
【leetcode】140.(Hard)Word Break II
解題思路: DP 首先建立一個boolean表用於記錄從字串哪些位置可以是一個word 然後使用DFS來遍歷結果 時間複雜度:O(n^2) 空間複雜度:O(n) 提交程式碼: class Solution{ public List<String> wordBreak
【LeetCode】#140單詞拆分II(Word Break II)
【LeetCode】#140單詞拆分II(Word Break II) 題目描述 給定一個非空字串 s 和一個包含非空單詞列表的字典 wordDict,在字串中增加空格來構建一個句子,使得句子中所有的單詞都在詞典中。返回所有這些可能的句子。 說明: 1.分隔時可以重複使用字典中的單詞
DP動態規劃專題五 :LeetCode 140. Word Break II
LeetCode 140. Word Break II Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to constr
【LeetCode】140. Word Break II 解題報告(Python & C++)
作者: 負雪明燭 id: fuxuemingzhu 個人部落格: http://fuxuemingzhu.cn/ 目錄 題目描述 題目大意 解題方法 遞迴求解 日期
LeetCode 140. Word Break II
問題 解法 dp , 狀態dp[i] = true 表示s.substr(i) 是能被分開. 遞推公式為: dp[i] = dp[i+1] && InDict(s.subs
LeetCode 140. 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.
LeetCode(140) 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
[Leetcode] 140. Word Break II 解題報告
題目: Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence
leetcode 140. Word Break II 深度優先搜尋DFS + 很棒的動態規劃DP 做法 + 記錄前驅節點
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence
LeetCode 140. Word Break II Solution 題解
Leetcode 140. Word Break II @(LeetCode)[LeetCode | Algorithm | DP | Backtracking | DFS] Given a non-empty string s and a dicti
140. Word Break II
chang init strings ats load == subst lis pac Given a non-empty string s and a dictionary wordDict containing a list of non-empty words,
140 Word Break II
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s&
leetcode140 Word Break II
思路: 直接爆搜會超時,需要使用記憶化搜尋。使用map把已經計算過的情況記錄下來,避免重複計算。 實現: 1 class Solution 2 { 3 public: 4 vector<string> wordBreak(string s, vector<str
leetcode word-break
別人很好的解題思路 https://www.nowcoder.com/questionTerminal/5f3b7bf611764c8ba7868f3ed40d6b2c 我的思路 package design; import java.util.HashSet; import java.util