Basic decomposition
Input
s = 'catsanddog', word_dict = ['cat', 'cats', 'and', 'sand', 'dog']
Output
['cat sand dog', 'cats and dog']
Two ways to split 'catsanddog': 'cat sand dog' and 'cats and dog'.
Full lesson preview
Given a string and a dictionary of words, return all possible space-separated sentences that form the string using dictionary words.
Problem statement
Task
Examples
Input
s = 'catsanddog', word_dict = ['cat', 'cats', 'and', 'sand', 'dog']
Output
['cat sand dog', 'cats and dog']
Two ways to split 'catsanddog': 'cat sand dog' and 'cats and dog'.
Input format
Output format
Constraints
Samples
Input
s = 'pineapplepenapple', word_dict = ['apple','pen','applepen','pine','pineapple']
Output
['pine apple pen apple', 'pine applepen apple', 'pineapple pen apple']
There are three valid sentences; the returned list is lexicographically sorted.