public static void wordBreak(String s, String[] dict, List<String> result) {
        // keep this boolean array for keeping isWord[j] if a word finishes at index j
        boolean[] isWord = new boolean[s.length() +1];
        Map<Integer, List<Integer>> rev = new HashMap<>();
        // for zero length string , we assume that its a valid word break
        isWord[0] = true;
        rev.put(new Integer(0), new ArrayList<Integer>());



        // go thrrough i  0 to length
        for(int i  = 0; i< s.length(); i++) {

            // go to first matching word, because we want to test continuity.
            if(!isWord[i]) {
                continue;
            }

            // iterate through all the words and check if any of them starts at i
            // if yes mark isWord[end] = true where end would be i + word.length
            for(String word : dict) {

                int end = i + word.length();

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

                if(s.substring(i, end).equals(word)) {
                    isWord[end] = true;
                    if(!rev.containsKey(new Integer(end))) {
                        rev.put(new Integer(end), new ArrayList<Integer>());
                    }
                    rev.get(new Integer(end)).add(new Integer(i));

                }
            }
        }

    }