Word Break-1

Hard
0/120
Average time to solve is 36m
profile
Contributed by
80 upvotes
Asked in companies
DunzoHikeApple

Problem statement

You are given a string 's', and a dictionary of words 'dict' containing 'n' words. Your task is to add spaces in 's' to form valid sentences, where each word is a word from the dictionary.


You need to return all possible sentences that can be formed using the given dictionary.


Note :
The same word from a dictionary can be used as many times as possible to make sentences.
Detailed explanation ( Input/output format, Notes, Images )
Input format :
The first line contains an integer value ‘n’ which denotes the size of the dictionary.
Next ‘n’ lines contains a non-empty string denoting words of dictionary.
Next (n+1)th line contains a non-empty string 's'.
Output format :
The output contains each possible sentence on a new line. If no such string can be formed then output is '-1'.
Note :
You don’t need to print anything. Just implement the given function.
Sample Input 1:
6
god
is
now
no
where
here
godisnowherenowhere
Sample Output 1:
god is no where no where
god is no where now here
god is now here no where
god is now here now here
Explanation to Sample Input 1:
One way to make sentences is to take “god” and append a space, then take “is”  and append space, take “now” from the dictionary and take “here” as well. 
Similarly, for other sentences, also we can add space to get other possible sentences.    
Note that you can reuse dictionary words as “no” and “now” are used two times to make the same sentence.
Sample Input 2:
4
god
is
no
here
godisnowhere
Sample Output 2:
-1
Explanation to Sample Input 2::
It is not possible to break the string 's' in a way that all the words are present in the dictionary 'dict' therefore output is -1.
Constraints :
1 <= n <= 1000
1 <= dic[i] <= 10
1 <= s.length <= 20

Time limit: 1 sec.
Hint

Can you think about exploring all the possibilities?

Approaches (3)
Brute Force Approach

We need to keep exploring the given string from current position ‘i’ to until we wouldn’t find a position such that substring ‘i’ to ‘j’ exists in the dictionary.

The algorithm looks like:

  1. Store all words of the dictionary in unordered_map to speed up checking whether a current word exists in the dictionary or not?
  2. If sentence size is ended then just return with a null string of list.
  3. Else,
    1. You start exploring every substring from the start of the string and check if it is in the dictionary.
      1. If it is
        1. Then you check if it is possible to form the rest of the sentence using dictionary words. If yes, you append the current substring to all the substring possible from the rest of the sentences.
    2. If none of the substrings of sentences are present in the dictionary, then there are no sentences possible from the current string.
Time Complexity

O(n*(2^n)), where ‘n’ is the length of the sentence.

Time complexity would be O(n*(2^n)) because every position we will have two choices, whether we can include it or not. And for a single option, we will have to put it into the list which will cost ‘n’.

Space Complexity

O(n*(2^n)), where ‘n’ is the length of the sentence.

Space complexity would be O(n*(2^n)) because in the worst case, we will have to store all possible combinations for all indexes to print the output. So there will be total ‘n’ indexes, and each index can have an O(2^n) combination of sentences. So overall space complexity will be O(n*(2^n)).

Code Solution
(100% EXP penalty)
Word Break-1
Full screen
Console