Word Break II

Hard
0/120
Average time to solve is 15m
profile
Contributed by
104 upvotes
Asked in companies
SalesforceTata Consultancy Services (TCS)Infosys

Problem statement

You are given a non-empty string S containing no spaces’ and a dictionary of non-empty strings (say the list of words). You are supposed to construct and return all possible sentences after adding spaces in the originally given string ‘S’, such that each word in a sentence exists in the given dictionary.

Note :

The same word in the dictionary can be used multiple times to make sentences.
Assume that the dictionary does not contain duplicate words.
Detailed explanation ( Input/output format, Notes, Images )

Input format :

The first line contains an integer ‘T’ denoting the number of test cases. 

Then the 'T' test cases follow.

The first line contains an integer value ‘K’ which denotes the size of the dictionary.

The second line contains ‘K’ non-empty, space separated strings denoting the words of the dictionary.

The third line contains a non-empty string ‘S’.

Output format :

For each test case, print each possible sentence after adding spaces, in different lines.

The output of each test case is printed in a separate line. 
Note :
1. You do not need to print anything, it has already been taken care of. Just implement the given function.
2. The order in which the output sentences are returned does not matter.
Constraints :
1 <= T <= 10
1 <= K <= 100
1 <= | word | <= 16
1 <= | S | <= 13

where |word| is the length of each word in the dictionary and |S| is the length of the string S.

Time Limit: 1 sec
Sample Input 1:
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 we can reuse dictionary words as “no” and “now” are used two times in the same sentence.
Sample Input 2:
1
4
god is no here
godisnowhere
Sample Output 2:
No output to be printed
Explanation to Sample Input 2:
We can not make any sentence because after making “god is no” we will be stuck with “where”. There is no way to break “where” further such that we can get any word from the dictionary.
Hint

Can you think about exploring all the possibilities?

Approaches (3)
Recursion

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.
 

Algorithm:

  • Store all words of the dictionary in a set to speed up the process of checking whether a current word exists in the dictionary or not.
  • Base condition, If we have checked for all the substrings, then just return with a list containing only a null string
  • Else,
    • Start exploring every substring from the start of the string and check if it is in the HashSet.
      • If it is present in the HashSet:
        • 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 substrings possible from the rest of the sentences.
    • If none of the substrings of sentences are present in the HashSet, then there are no sentences possible from the current string.
  • Return all the valid output sentences.
Time Complexity

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

For each space between 2 characters, we can either break the word there or not. Thus, the number of maximum word breaks is 2 ^ (N - 1) (because the string of length N will have N - 1 spaces between 2 characters).

And for a single option, we will have to put it into the list which will cost ‘N’. Thus, the final time complexity would be O(N * (2 ^ N)).

Space Complexity

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

In the worst case, we will have to store all possible combinations for all indexes to print the output. There are O(2 ^ (N - 1)) combinations of sentences and the length of the sentence will be somewhere between N and 2 * N. So, the overall space complexity will be O(N * (2 ^ (N - 1))).

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