`

Leetcode - WordBreak III

 
阅读更多

Given a string s and a dictionary of words dict, determine if s can be segmented into a sequence of one or more dictionary words.

   But notice that each word in the dictionary can be used at most once.

For example:

  the dictionary is {"leet", "co" "de" "code"}

  It can match "leetcode", can match “leetcodecode”,but can’t match “leetleetcode”
 [分析] 此题为yb君改编,改编后的题目不能采用动态规划来做,动态规划要求问题的各求解方案是独立平行的,改编后一步决策会影响下一步,因此需要回溯。思维混乱或者貌似空白时把每一点想法尽可能的描述出来,这非常有助于形成完整的思路。

 

boolean IsComposible(Set<String> dict, String s) {
	if (dict == null || s == null || s.length() == 0)
		return false;
	if (dict.contains(s))
		return true;
	int n = s.length();
	for (int i = 1; i <= n; i++) {
		String substr = s.substring(0, i);
		if (dict.contains(substr)) {
			dict.remove(substr);
			if (isComposible(dict, s.substring(i, n)))
				return true;
			dict.add(substr);
		}
	}
	return false;
}

// 下面代码为扩展题,给出一种方案
ArrayList<String> IsComposible(Set<String> dict, String s) {
	ArrayList<String> result = new ArrayList<String>();
	dowork(dict, s, result);
	return result;
}

boolean dowork(Set<String> dict, String s, ArrayList<String> comp) {
	if (s == null || s.length() == 0)
		return true;
	if (dict == null)
		return false;
	
	int n = s.length();
	for (int i = 1; i <= n; i++) {
		String substr = s.substring(0, i);
		if (dict.contains(substr)) {
			dict.remove(substr);
			comp.add(substr);
			if (dowork(dict, s.substrin(i, n), comp))
				return true;
			dict.add(substr);
			comp.removeAt(comp.size() - 1);
		}
	}
	return false;
}

 

分享到:
评论

相关推荐

    python-leetcode题解之139-Word-Break

    针对LeetCode中的第139题“Word Break”(单词拆分),Python解决方案提供了一种高效而优雅的编码实现方式,对于掌握动态规划、回溯算法和哈希表等编程概念具有重要的意义。 “Word Break”问题是一个典型字符串...

    python-leetcode题解之140-Word-Break-II

    本篇内容专注于解决 LeetCode 上的第140号问题——Word Break II。这是一道涉及到字符串处理、递归、回溯以及动态规划的问题。问题的难点在于不仅要判断能否将给定字符串按给定字典进行拆分,还要输出所有可能的拆分...

    js-leetcode题解之139-word-break.js

    LeetCode题号139的这道“Word Break”题便是这样一个问题。该问题要求实现一个函数,该函数接收两个参数:一个字符串s和一个字符串数组wordDict作为字典,返回一个布尔值,表示是否可以将字符串s拆分为字典中的单词...

    js-leetcode题解之140-word-break-ii.js

    在解决"Word Break II"这一经典的编程问题时,LeetCode平台上的题解提供了一种使用JavaScript语言实现的解决方案。这个问题要求编程者设计一个算法,能够找出所有可能的单词断句方式,并返回这些断句方式组成的句子...

    wordbreakleetcode-WordBreak-Leetcode-139:一种使用动态规划来确定一个词是否可以作为给定词典中所有词的串

    在提供的代码库"WordBreak-Leetcode-139-master"中,你可能会找到一个具体的实现,它可能包括以下部分: - 主函数,接收字符串sentence和词汇表wordDict作为输入参数。 - 动态规划的逻辑,包括初始化dp数组和遍历...

    lrucacheleetcode-leetcode-in-go:leetcode-in-go

    lru缓存leetcode Go 中解决的一些 Leetcode 问题 大批 3sum-0015 买卖股票的最佳时机-0121 最多水的容器-0011 contains-duplicate-0217 find-minimum-in-rotated-sorted-array-0153 ...word-break-0139 图形

    leetcode苹果-word-break:断字

    leetcode 苹果断字 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,确定 s 是否可以被分割成一个或多个字典单词的空格分隔序列。 笔记: 字典中的同一个词可能会在切分中重复使用多次。...word. E

    扩展矩阵leetcode-Leetcode:LeetcodeAnswer-Java

    扩展矩阵leetcode Leetcode Leetcode Answer-Java 数组 11.乘最多水容器 ...wordBreak 279.完全平方数 numSquares 排序 56.合并区间 merge 75.颜色分类 sortColors 179.最大数 largestNumber 324.摆

    LeetCode题解 - Java语言实现-181页.pdf

    4. Solution Word Break 单词断词是一个自然语言处理问题,要求将字符串断词为单词。可以使用动态规划或Trie树来解决该问题。 5. Word Break II 单词断词II是一个变体的问题,要求将字符串断词为单词,并且可以...

    LeetCode最全代码

    260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [C++](./C++/single-number-iii.cpp) [Python](./Python/single-number-iii.py) | _O(n)_ | _O(1)_ | Medium || 268| [Missing ...

    javascript-leetcode面试题解动态规划问题之第139题单词拆分-题解.zip

    function wordBreak(s, wordDict) { const dp = Array(s.length + 1).fill(false); dp[0] = true; for (let i = 1; i ; i++) { for (const word of wordDict) { if (s.startsWith(word, i - word.length) && ...

    wordbreakleetcode-WordBreak:“断字”问题的解决方案。用C#编写

    断字leetcode 断字 “断字”问题的解决方案。 用 C# 编写 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. ...

    leetcode530-leetcode:力扣在线评委

    leetcode 530 力扣在线评委 # 问题 困难 解决方案 1 简单的 2 中等的 3 中等的 12 中等的 22 中等的 39 中等的 94 中等的 108 中等的 122 中等的 136 中等的 144 中等的 167 中等的 238 中等的 260 中等的 268 中等...

    leetcode338-coding_notebook:编码_笔记本

    Break](Leetcode 问题/数组和字符串/139.word_break.md) [140. Word Break ii](Leetcode 问题/数组和字符串/140.word_break_ii.md) [151. 反转字符串中的单词](Leetcode Problems/Array and String/151.reverse_...

    LeetCode题解(java语言实现).pdf

    * Word Break、Word Break II:该题目要求将字符串分割成单词,实现方法使用了动态规划和回溯算法。 * Word Ladder:该题目要求将一个单词转换成另一个单词,实现方法使用了广度优先搜索算法。 二、树和图 * ...

    Leetcode题目+解析+思路+答案.pdf

    - **Word Break**:判断一个字符串是否可以拆分为一个词汇表中的单词序列。 7. **链表(Linked List)**: - **Linked List Cycle**:检测链表中的环。 - **Remove Duplicates from Sorted List**:从已排序的...

    LeetCode:LeetCode解决方案

    preorder-traversal链表reorder-list链表linked-list-cycle-ii链表linked-list-cycle动态规划word-break-ii动态规划word-break链表copy-list-with-random-pointer复杂度single-number-ii复杂度single-number动态规划

    python-leetcode面试题解之第139题单词拆分-题解.zip

    def wordBreak(s, wordDict): wordDict = set(wordDict) # 将单词列表转换为集合,加快查找速度 dp = [False] * (len(s) + 1) # 初始化动态规划数组,dp[i]表示s[:i]是否可以被拆分 dp[0] = True # 空字符串可以...

    gasstationleetcode-leetcode:LeetcodeOJ解决方案

    leetcode 【演示记录】 报告 展示 2017/03/06 1.二和,167.二和二 2107/03/06 15.3 总和,16.3 总和最近,18.4 总和,11.最多水的容器 2017/03/09 62.Unique Paths, 63.Unique Paths II, 64.Minimum Path Sum 2017/...

    leetcode刷题之动态规划

    var wordBreak = function(s, wordDict) { let n = s.length; let dp = new Array(n + 1).fill(false); dp[0] = true; for (let i = 0; i ; i++) { for (let j = 0; j ; j++) { if (dp[j] && wordDict....

Global site tag (gtag.js) - Google Analytics