From fb8bc342df56bd5d4c44d579294c87c703dc5b95 Mon Sep 17 00:00:00 2001 From: JoannaCode Date: Sat, 16 Apr 2016 23:56:21 -0700 Subject: [PATCH 1/2] add 140 --- Hard/WordBreakII.java | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Hard/WordBreakII.java diff --git a/Hard/WordBreakII.java b/Hard/WordBreakII.java new file mode 100644 index 00000000..c5498e05 --- /dev/null +++ b/Hard/WordBreakII.java @@ -0,0 +1,57 @@ +import java.util.*; + +/** + * 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"]. + * Tags: Dynamic Programming Backtracking + * @author chenshuna + */ + +public class WordBreakII { + static HashMap> map = new HashMap<>(); + + public static List wordBreak(String s, Set wordDict) { + int maxLengthOfWordDict = 0; + for(String str : wordDict) maxLengthOfWordDict = Math.max(maxLengthOfWordDict, str.length()); + return helper(s, wordDict, 0, maxLengthOfWordDict); + } + + public static List helper(String s, Set wordDict, int start, int max){ + List res = new ArrayList<>(); + if(start == s.length()) { + res.add(""); + return res; + } + for(int i = start + 1 ; i <= max + start && i <= s.length(); i++){ + String temp = s.substring(start, i); + if(wordDict.contains(temp)){ + List l; + if(map.containsKey(i)) + l = map.get(i); + else + l = helper(s, wordDict, i, max); + for(String ss : l){ + res.add(temp + (ss.equals("") ? "" : " ") + ss); + } + } + } + map.put(start, res); + return res; + } + + public static void main(String[] args) { + Set wordDict = new HashSet(); + wordDict.add("cat"); + wordDict.add("cats"); + wordDict.add("and"); + wordDict.add("sand"); + wordDict.add("dog"); + String s = "catsanddog"; + System.out.print(wordBreak(s, wordDict)); + } +} \ No newline at end of file From fd72a52120b02c465d917eca2c1647154931cb7b Mon Sep 17 00:00:00 2001 From: JoannaCode Date: Sat, 16 Apr 2016 23:58:21 -0700 Subject: [PATCH 2/2] add 140 --- Hard/WordBreakII.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Hard/WordBreakII.java b/Hard/WordBreakII.java index c5498e05..69389727 100644 --- a/Hard/WordBreakII.java +++ b/Hard/WordBreakII.java @@ -17,7 +17,8 @@ public class WordBreakII { public static List wordBreak(String s, Set wordDict) { int maxLengthOfWordDict = 0; - for(String str : wordDict) maxLengthOfWordDict = Math.max(maxLengthOfWordDict, str.length()); + for(String str : wordDict) + maxLengthOfWordDict = Math.max(maxLengthOfWordDict, str.length()); return helper(s, wordDict, 0, maxLengthOfWordDict); } @@ -35,15 +36,14 @@ public static List helper(String s, Set wordDict, int start, int l = map.get(i); else l = helper(s, wordDict, i, max); - for(String ss : l){ + for(String ss : l) res.add(temp + (ss.equals("") ? "" : " ") + ss); - } } } map.put(start, res); return res; } - + public static void main(String[] args) { Set wordDict = new HashSet(); wordDict.add("cat");