首页>itarticle>leetcode(140): word break(2) Solution
leetcode(140): word break(2) Solution
admin11月 12, 20200
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. You may assume the dictionary does not contain duplicate words.
Return all such possible sentences.
For example, given s = "catsanddog", dict = ["cat", "cats", "and", "sand", "dog"].
A solution is ["cats and dog", "cat sand dog"].
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
publicclass{
public List<String> wordBreak(String s, List<String> wordDict){
return dfs(s, wordDict, new HashMap<String, LinkedList<String>>());
近期评论