Java for LeetCode 140 Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.

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"].

解题思路:

参考上题思路二的解法,修改下代码,让其显示路径即可,JAVA实现如下:

static public List<String> wordBreak(String s, Set<String> wordDict) {
		List<String> list = new ArrayList<String>();
		dfs(list, s, wordDict, new HashSet<String>(), new ArrayList<String>());
		return list;
	}

	static public boolean dfs(List<String> list, String s, Set<String> dict,
			Set<String> unmatch, List<String> alist) {
		boolean isMatch=false;
		for (String prefix : dict) {
			if (s.equals(prefix)) {
				alist.add(prefix);
				StringBuilder sb = new StringBuilder();
				for (String str : alist) {
					sb.append(str);
					sb.append(" ");
				}
				list.add(sb.substring(0, sb.length() - 1));
				alist.remove(alist.size() - 1);
				isMatch=true;
				continue;
			} else if (s.startsWith(prefix)) {
				String suffix = s.substring(prefix.length());
				if (!unmatch.contains(suffix)) {
					alist.add(prefix);
					if (!dfs(list, suffix, dict, unmatch, alist))
						unmatch.add(suffix);
					else isMatch=true;
					alist.remove(alist.size() - 1);
				}
			}
		}
		return isMatch;
	}
时间: 2024-10-10 18:55:39

Java for LeetCode 140 Word Break II的相关文章

leetcode[140] Word Break II

这题是上一题的加强版,这里需要返回的是所有可能的分割词.例如: s = "catsanddog",dict = ["cat", "cats", "and", "sand", "dog"]. A solution is ["cats and dog", "cat sand dog"]. 先用dp求得每个起点到终点是否符合word break 1中的条

leetcode 140. Word Break II ----- java

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"

【leetcode】Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"

Leetcode dfs Word Break II

Word Break II Total Accepted: 15138 Total Submissions: 92228My Submissions Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For ex

Java for LeetCode 212 Word Search II

Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same le

[C++]LeetCode: 113 Word Break II (DP &amp;&amp; Backtacking) 求解拆分组合

题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "catsanddog", dict = ["cat", "cats

【LeetCode】Word Break II 解题报告

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "catsanddog", dict = ["cat", "cats&quo

【LeetCode】Word Break II 动态规划

题目:Word Break 要求找到所有能够有字典中的词重组成目标串的结果 public class Solution { public static List<String> wordBreak(String s, Set<String> dict) { List<String> dp[] = new ArrayList[s.length()+1]; dp[0] = new ArrayList<String>(); for(int i=0; i<s.

[leedcode 140] Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"