package com.fishercoder.solutions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 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 _140 {
    public List<String> wordBreak(String s, Set<String> wordDict) {
        return dfs(s, wordDict, new HashMap<String, ArrayList<String>>());
    }

    private List<String> dfs(String s, Set<String> wordDict,
                             HashMap<String, ArrayList<String>> map) {
        if (map.containsKey(s)) {
            return map.get(s);
        }

        ArrayList<String> res = new ArrayList<String>();
        if (s.length() == 0) {
            res.add("");
            return res;
        }

        for (String word : wordDict) {
            if (s.startsWith(word)) {
                List<String> subList = dfs(s.substring(word.length()), wordDict, map);
                for (String sub : subList) {
                    res.add(word + (sub.length() == 0 ? "" : " ") + sub);
                }
            }
        }
        map.put(s, res);
        return res;
    }

    public static void main(String... strings) {
        List<String> temp = new ArrayList<String>();
        System.out.println(temp);
        List<String> temp2 = new ArrayList<String>(temp);
        temp2.add("");
        System.out.println(temp2);

        _140 test = new _140();
        Set<String> wordDict = new HashSet();
        wordDict.add("cat");
        wordDict.add("cats");
        wordDict.add("sand");
        wordDict.add("and");
        wordDict.add("dog");
        String s = "catsanddog";
//        List<String> list = test.wordBreak(s, wordDict);
        List<String> list = test.wordBreak(s, wordDict);
        for (String word : list) {
            System.out.print(word + ", ");
        }
        System.out.println();
    }
}

@@ -104,65 +104,42 @@
 til.
-HashSet;%0Aimport java.util.List;%0Aimport java.util.Set;%0A%0A/*
+List;%0A%0A/**%0A * 140. Word Break II%0A 
 *%0A *
@@ -508,34 +508,35 @@
 Break(String s, 
-Se
+Lis
 t%3CString%3E wordDi
@@ -589,33 +589,8 @@
 Map%3C
-String, ArrayList%3CString%3E
 %3E())
@@ -606,16 +606,8 @@
     
-private 
 List
@@ -629,18 +629,19 @@
 ring s, 
-Se
+Lis
 t%3CString
@@ -655,37 +655,8 @@
 ict,
-%0A                            
  Has
@@ -668,21 +668,16 @@
 String, 
-Array
 List%3CStr
@@ -794,16 +794,19 @@
 ing%3E res
+ult
  = new A
@@ -806,38 +806,32 @@
 = new ArrayList%3C
-String
 %3E();%0A        if 
@@ -861,24 +861,27 @@
          res
+ult
 .add(%22%22);%0A  
@@ -896,24 +896,27 @@
   return res
+ult
 ;%0A        %7D%0A
@@ -1148,16 +1148,19 @@
      res
+ult
 .add(wor
@@ -1270,787 +1270,41 @@
  res
-);%0A        return res;%0A    %7D%0A%0A    public static void main(String... strings) %7B%0A        List%3CString%3E temp = new ArrayList%3CString%3E();%0A        System.out.println(temp);%0A        List%3CString%3E temp2 = new ArrayList%3CString%3E(temp);%0A        temp2.add(%22%22);%0A        System.out.println(temp2);%0A%0A        _140 test = new _140();%0A        Set%3CString%3E wordDict = new HashSet();%0A        wordDict.add(%22cat%22);%0A        wordDict.add(%22cats%22);%0A        wordDict.add(%22sand%22);%0A        wordDict.add(%22and%22);%0A        wordDict.add(%22dog%22);%0A        String s = %22catsanddog%22;%0A//        List%3CString%3E list = test.wordBreak(s, wordDict);%0A        List%3CString%3E list = test.wordBreak(s, wordDict);%0A        for (String word : list) %7B%0A            System.out.print(word + %22, %22);%0A        %7D%0A        System.out.println();%0A    %7D
+ult);%0A        return result;%0A    %7D%0A
 %0A%7D
-%0A

refactor 140