【LeetCode笔记】Word Break
来源:互联网 发布:mac系统安装word文档 编辑:程序博客网 时间:2024/05/07 01:00
The following solution is by shibai86
Bottom up DP
class Solution {public: bool wordBreak(string s, unordered_set<string> &dict) { int len = s.length(); vector<bool> dp(len + 1,false); dp[len] = true; for (int i = len - 1; i >= 0; i--) { for (int j = i; j < len; j++) { string str = s.substr(i,j - i + 1); if (dict.find(str) != dict.end() && dp[j + 1]) { dp[i] = true; break; } } } return dp[0]; }};
edit: dp[i] has the boolean value that means if string[i : n] can be partitioned according to our dictionary. Hence dp[len] represents the empty string and dp[0] is the answer we are looking for.
For each iteration in the inner for loop, we add a new cut at index j to string[i : n], in whcih string[i : j] has never checked before but string[j + 1 : n](result is in dp[j + 1]) has been known since the previous iteration in outer loop.
0 0
- 【LeetCode笔记】Word Break
- leetcode笔记:Word Break
- leetcode Word Break & Word Break ||
- Leetcode: Word Break
- [leetcode]Word Break
- [leetcode]Word Break II
- LeetCode:Word Break
- LeetCode:Word Break II
- Leetcode: Word Break II
- [LeetCode] Word Break
- [LeetCode] Word Break II
- LeetCode: Word Break
- leetcode之Word Break
- [LeetCode]Word Break II
- [LeetCode]Word Break
- leetcode-Word Break
- LeetCode 之 Word Break
- leetcode word break
- thinkphp查询返回简单字段数组例子详解
- 退耦电容
- SendEmail C#
- Reverse Linked List II
- Eclipse jad插件安装完出现source not found 解决方法
- 【LeetCode笔记】Word Break
- jqgrid
- Android保持屏幕常亮
- Pig、Hive、MapReduce 解决分组 Top K 问题
- 附近辅导员余土同意
- AAC帧格式及编码介绍
- 20140826 N2
- 黑马程序员——java基础之类集框架
- 银行存款业务