Practice/Meta/Leetcode 140. Word Break II
CodingMust
You are given a string s and a list of words wordDict representing a dictionary. Your task is to find all possible ways to reconstruct the string s by combining words from the dictionary with spaces between them.
Each word from the dictionary can be used multiple times, and you must return a list of all valid sentence reconstructions. If no valid reconstruction exists, return an empty list.
The order of sentences in the output does not matter, but each word in a sentence must be separated by a single space.
s and wordDict[i] consist of only lowercase English letterswordDict are uniqueExample 1:
` Input: s = "catsanddog", wordDict = ["cat", "cats", "and", "sand", "dog"] Output: ["cat sand dog", "cats and dog"] Explanation:
Example 2:
Input: s = "pineapplepenapple", wordDict = ["apple", "pen", "applepen", "pine", "pineapple"] Output: ["pine apple pen apple", "pine applepen apple", "pineapple pen apple"] Explanation: Multiple valid ways to segment using different word combinations.
Example 3:
Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] Output: [] Explanation: No valid segmentation exists because 'sandog' cannot be formed from dictionary words.