Problem of the day
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.
The same word from a dictionary can be used as many times as possible to make sentences.
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.
6
god
is
now
no
where
here
godisnowherenowhere
god is no where no where
god is no where now here
god is now here no where
god is now here now here
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.
4
god
is
no
here
godisnowhere
-1
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.
1 <= n <= 1000
1 <= dic[i] <= 10
1 <= s.length <= 20
Time limit: 1 sec.