[LeetCode] Word Break II
来源:互联网 发布:c语言中pcos什么意识 编辑:程序博客网 时间:2024/06/05 14:22
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"]
.
class Solution {public: vector<string> wordBreak(string s, unordered_set<string> &dict) { int len = s.size(); vector<vector<bool>> flag(len, vector<bool>(len, false)); for(int i = 0; i < len; i++) { for(int j = 0; j < len; j++) { if(dict.find(s.substr(i,j-i+1)) != dict.end()) flag[i][j] = true; } } vector<string> result; for(int i = len-1; i >= 0; i--) { if(flag[i][len-1]) { string str = s.substr(i, len-i); back(flag, i-1, s, str, result); } } return result; } void back(vector<vector<bool>> &flag, const int &i, const string &s, string str, vector<string> &result) { if(i==-1) { result.push_back(str); return; } for(int j = 0; j <= i; j++) { if(flag[j][i]) { string cur_str = s.substr(j, i-j+1) + " " + str; back(flag, j-1, s, cur_str, result); } } }};
0 0
- [leetcode]Word Break II
- LeetCode:Word Break II
- Leetcode: Word Break II
- [LeetCode] Word Break II
- [LeetCode]Word Break II
- LeetCode | Word Break II
- [LeetCode] - Word Break II
- LeetCode - Word Break II
- Leetcode Word Break II
- [LeetCode] Word Break II
- Word Break II -- LeetCode
- LeetCode (Word Break II )
- Leetcode--Word Break II
- Leetcode: Word Break II
- LeetCode Word Break II
- Word Break II leetcode
- [leetcode]Word Break II
- leetcode -- word break II
- 随机字符串
- 关于StoryBoard的一些用法研究
- record
- 基于SVN分支开发模式流程浅析
- 堆变量和栈变量
- [LeetCode] Word Break II
- java西天取经之路(二)Servlet部署和配置
- 安卓处理Html----图片异步加载
- 写了单链表的实现
- 【MongoDB】windows下搭建Mongo主(Master)/从(slave)数据库同步
- BeagleBone Black Cross-compiler environment
- [LeetCode] Distinct Subsequences
- mdev.c分析(一)
- 单例模式内容