Class Solution

java.lang.Object
g0101_0200.s0140_word_break_ii.Solution

public class Solution extends Object
140 - Word Break II.<p>Hard</p> <p>Given a string <code>s</code> and a dictionary of strings <code>wordDict</code>, add spaces in <code>s</code> to construct a sentence where each word is a valid dictionary word. Return all such possible sentences in <strong>any order</strong>.</p> <p><strong>Note</strong> that the same word in the dictionary may be reused multiple times in the segmentation.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;catsanddog&rdquo;, wordDict = [&ldquo;cat&rdquo;,&ldquo;cats&rdquo;,&ldquo;and&rdquo;,&ldquo;sand&rdquo;,&ldquo;dog&rdquo;]</p> <p><strong>Output:</strong> [&ldquo;cats and dog&rdquo;,&ldquo;cat sand dog&rdquo;]</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;pineapplepenapple&rdquo;, wordDict = [&ldquo;apple&rdquo;,&ldquo;pen&rdquo;,&ldquo;applepen&rdquo;,&ldquo;pine&rdquo;,&ldquo;pineapple&rdquo;]</p> <p><strong>Output:</strong> [&ldquo;pine apple pen apple&rdquo;,&ldquo;pineapple pen apple&rdquo;,&ldquo;pine applepen apple&rdquo;]</p> <p><strong>Explanation:</strong> Note that you are allowed to reuse a dictionary word.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;catsandog&rdquo;, wordDict = [&ldquo;cats&rdquo;,&ldquo;dog&rdquo;,&ldquo;sand&rdquo;,&ldquo;and&rdquo;,&ldquo;cat&rdquo;]</p> <p><strong>Output:</strong> []</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 20</code></li> <li><code>1 <= wordDict.length <= 1000</code></li> <li><code>1 <= wordDict[i].length <= 10</code></li> <li><code>s</code> and <code>wordDict[i]</code> consist of only lowercase English letters.</li> <li>All the strings of <code>wordDict</code> are <strong>unique</strong>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details