You are given a non-empty string containing no spaces (say sentence) and a dictionary of list 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 (sentence), where each word must exist in the given dictionary.
The same word from a dictionary can be used as many times as possible to make sentences.
Input format :
The first line contains an integer value ‘N’ which denotes the size of the dictionary.
Next ‘N’ line contains a non-empty string “denoting words”.
Next ‘(N+1)’ th line contains a non-empty string without any space.
Output format :
Print each possible sentence after adding spaces in different lines.
You do not need to print anything, it has already been taken care of. Order of sentences does not matter.
1 <= N <= 100
1 <= M <= 16
1 <= W <= 16
Where ‘N’ is the length of a dictionary , ‘W’ is the length of the “word” in a dictionary and ‘M’ is the length of a sentence.
Time limit: 1 sec.
Sample Input 1:
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:
Sample Output 2:
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”.