package com.ykushch.prjalgo2.task1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import static java.util.stream.Collectors.joining;

public class WordBreak {

    /**
     * Breaks words using naive approach (plain recursion)
     * @param s sentence without spaces
     * @param dict dictionary with unique words
     * @return sentence with spaces based on dictionary
     */
    public String breakWordRecursively(String s, Set<String> dict) {
        List<String> resultList = new ArrayList<>();
        if (!couldBreakSentenceRecursively(s, dict, 0, resultList)) {
            return "";
        }
        Collections.reverse(resultList);
        return resultList.stream()
                .collect(joining(" "));
    }

    private boolean couldBreakSentenceRecursively(String s, Set<String> dict, int start, List<String> resultList) {
        if(start == s.length()) {
            return true;
        }

        for(String a: dict) {
            int len = a.length();
            int end = start + len;

            if(end > s.length()) {
                continue;
            }

            if(s.substring(start, end).equals(a)) {
                if(couldBreakSentenceRecursively(s, dict, start + len, resultList)) {
                    resultList.add(s.substring(start, start + len));
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Breaks words using dynamic programming approach
     * @param s sentence without spaces
     * @param dict dictionary with unique words
     * @return sentence with spaces based on dictionary
     */
    public String breakWord(String s, Set<String> dict) {
        List<String> result = breakDownSentence(s, dict);

        if (result.isEmpty()) {
            return "";
        }
        return result.stream()
                .collect(joining(" "));
    }

    private List<String> breakDownSentence(String sentence, Set<String> dict) {
        List<String> resultBreakDownList = new ArrayList<>();
        boolean[] result = new boolean[sentence.length() + 1];
        result[0] = true;

        for (int i = 0; i < sentence.length(); i++) {
            if(!result[i]) {
                continue;
            }

            for (String wordFromDict : dict) {
                int len = wordFromDict.length();
                int end = i + len;

                if(end > sentence.length() || result[end]) {
                    continue;
                }

                if(sentence.substring(i, end).equals(wordFromDict)) {
                    resultBreakDownList.add(wordFromDict);
                    result[end] = true;
                }
            }
        }

        return resultBreakDownList;
    }
}

@@ -286,32 +286,39 @@
 %0A     * @param s
+entence
  sentence withou
@@ -477,32 +477,39 @@
 rsively(String s
+entence
 , Set%3CString%3E di
@@ -579,13 +579,8 @@
     
-if (!
 coul
@@ -598,32 +598,39 @@
 nceRecursively(s
+entence
 , dict, 0, resul
@@ -639,44 +639,9 @@
 ist)
-) %7B%0A            return %22%22;%0A        %7D
+;
 %0A   
@@ -810,32 +810,39 @@
 rsively(String s
+entence
 , Set%3CString%3E di
@@ -855,16 +855,66 @@
 t start,
+%0A                                                 
  List%3CSt
@@ -954,16 +954,23 @@
 art == s
+entence
 .length(
@@ -1001,33 +1001,32 @@
 true;%0A        %7D%0A
-%0A
         for(Stri
@@ -1028,17 +1028,20 @@
 (String 
-a
+word
 : dict) 
@@ -1064,17 +1064,20 @@
 t len = 
-a
+word
 .length(
@@ -1106,33 +1106,32 @@
  = start + len;%0A
-%0A
             if(e
@@ -1136,16 +1136,23 @@
 (end %3E s
+entence
 .length(
@@ -1213,42 +1213,99 @@
     
-if(s.substring(start, end)
+String extractedWord = sentence.substring(start, end);%0A            if(extractedWord
 .equals(
 a)) 
@@ -1300,17 +1300,20 @@
 .equals(
-a
+word
 )) %7B%0A   
@@ -1359,16 +1359,23 @@
 sively(s
+entence
 , dict, 
@@ -1438,16 +1438,23 @@
 st.add(s
+entence
 .substri
@@ -1916,74 +1916,8 @@
 ct);
-%0A%0A        if (result.isEmpty()) %7B%0A            return %22%22;%0A        %7D
 %0A   
@@ -2605,19 +2605,39 @@
         
-if(
+String extractedWord = 
 sentence
@@ -2654,16 +2654,50 @@
 (i, end)
+;%0A                if(extractedWord
 .equals(

Refactor names