mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 03:33:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			38 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			38 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<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> </p>
 | 
						|
<p><strong>Example 1:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> s = "catsanddog", wordDict = ["cat","cats","and","sand","dog"]
 | 
						|
<strong>Output:</strong> ["cats and dog","cat sand dog"]
 | 
						|
</pre>
 | 
						|
 | 
						|
<p><strong>Example 2:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> s = "pineapplepenapple", wordDict = ["apple","pen","applepen","pine","pineapple"]
 | 
						|
<strong>Output:</strong> ["pine apple pen apple","pineapple pen apple","pine applepen apple"]
 | 
						|
<strong>Explanation:</strong> Note that you are allowed to reuse a dictionary word.
 | 
						|
</pre>
 | 
						|
 | 
						|
<p><strong>Example 3:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
 | 
						|
<strong>Output:</strong> []
 | 
						|
</pre>
 | 
						|
 | 
						|
<p> </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>
 |