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.

For example, given
s = "leetcode",
dict = ["leet", "code"].

Return true because "leetcode" can be segmented as "leet code".

解题思路:

问题: 根据给定的单词字典,判断一个字符串是否可以全部被分割为有效单词。

第一个问题,给定一个字符串, 怎么判断它是不是一个有效单词呢?

本人首先想到是之前做过的 Trie Tree ,打算用 Trie Tree 结构来判断一个字符串是不是一个有效单词。

后来才知道, unordered_set 是 C++ 自带的数据结构,能直接用来检索一个字符串是否为有效单词。囧。看来对 C++ 还不够熟悉。

值得一提的是,用 Trie Tree 实现的算法也通过了 LeetCode 的测试,当然,unordered_set 的完成效率更快。

第二个问题,如何判断一个字符串是否可以全部被分割为有效单词,即原问题。

假设将字符串 s 分割为两段,[0,i-1], [i, n-1],如果[0, i-1] 为有效单词,[i, n-1]为有效单词集合,那么 s 就是一个有效字符串。

将 i 从 1 到 n-1 遍历一次,求得 s 是否是一个有效字符串。

第三个问题,效率满,出现很多反复求解的子问题。

DP思路,即表格法,记录已计算结果。

    vector<int> v;

    bool isMatch(string s, unordered_set<string>& wordDict){

        unordered_set<string>::iterator us_iter = wordDict.find(s);

        if (us_iter != wordDict.end()) {
return true;
} for (int i = ; i < s.size(); i++) {
string leftS = s.substr(, i); unordered_set<string>::iterator leftS_iter = wordDict.find(leftS);
if (leftS_iter != wordDict.end()) { bool isWordR;
if (v[i] != -) {
isWordR = v[i];
}else{
isWordR = isMatch(s.substr(i, s.size() - i), wordDict);
v[i] = isWordR;
} if (isWordR) {
return true;
}
}
} return false;
} bool wordBreak(string s, unordered_set<string>& wordDict) {
vector<int> tmp(s.size(), -); v = tmp;
bool res = isMatch(s, wordDict); return res;
}

[LeetCode] Word Break 解题思路的更多相关文章

  1. [LeetCode] Word Break II 解题思路

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

  2. LeetCode: Word Break II 解题报告

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  3. 【LeetCode】139. Word Break 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. [leetcode]Word Break II @ Python

    原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words  ...

  5. [LeetCode] Word Break II 拆分词句之二

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

  6. LeetCode ||& Word Break && Word Break II(转)——动态规划

    一. Given a string s and a dictionary of words dict, determine if s can be segmented into a space-sep ...

  7. LeetCode:Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  8. LeetCode Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words  ...

  9. [leetcode]Word Break @ Python

    原题地址:https://oj.leetcode.com/problems/word-break/ 题意: Given a string s and a dictionary of words dic ...

随机推荐

  1. C语言开发环境配置

    链接:http://pan.baidu.com/s/1qWkpD72 密码:zhig 将解压包直接解压放在C盘下. 右击我的电脑,点属性—>高级—>环境变量然后在PATH里加入C:\Min ...

  2. javascript入门学习笔记2

    JavaScript 拥有动态类型.这意味着相同的变量可用作不同的类型: 实例 var x // x 为 undefined var x = 6; // x 为数字 var x = "Bil ...

  3. java设计模式——接口模式

    java将接口的概念提升为独立的结构,体现了接口与实现分离.java接口允许多个类提供相同的功能,也允许一个同时实现多个接口.java的接口与抽象类十分相似.java与抽象类中的区别: 1.一个类可以 ...

  4. USACO 2.2 Subset Sums 集合(subset)

    Description 对于从1到N的连续整集合,能划分成两个子集合,且保证每个集合的数字和是相等的.举个例子,如果N=3,对于{1,2,3}能划分成两个子集合,他们每个的所有数字和是相等的: {3} ...

  5. HDU 5491 The Next

    Problem Description Let L denote the number of 1s in integer D’s binary representation. Given two in ...

  6. demo_02 less

    html 中的代码<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> &l ...

  7. 精通 Oracle+Python,第 4 部分:事务和大型对象

    通过 Python 管理数据事务.处理大型对象 2010 年 3 月发布 事务包含一组 SQL 语句,这组 SQL 语句构成数据库中的一个逻辑操作,如转帐或信用卡支付操作.将 SQL 语句聚合到一个逻 ...

  8. Django 1

    Django 1.10文档中文版Part1 本文是博主翻译的Django1.10版本官方文档的第一部分,如时间充裕,争取一直翻译下去,经验不足,或有错漏,敬请指正.另外对于公开文档进行翻译的版权问题不 ...

  9. 关于Android中传递数据的一些讨论--备用

    在Android中编写过程序的开发人员都知道.在Activity.Service等组件之间传递数据(尤其是复杂类型的数据)很不方便.一般可以使用Intent来传递可序列化或简单类型的数据.看下面的代码 ...

  10. css3实现无缝滚动效果

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...