[LeetCode]Word BreakII
来源:互联网 发布:网络机顶盒怎么破解 编辑:程序博客网 时间:2024/09/21 09:24
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"]
.
For the "Return all " problems, usually DFS or BFS will work well.
In this problem, a DFS with a simple cutting edge condition will pass all the test cases.
The idea is to use mp[][] array to record which substring is in dict. and a array to record the insert index
for those substrings, we use DFS.
c++
class Solution {public: void dfsWordBreak(string &s, int st, vector<int>&sp, unordered_set<string> &dict, vector<string> &result, vector<vector<bool>> &mp){ if(st>=s.size()){ string str = s; for(int i=0;i<sp.size()-1;i++){ str.insert(sp[i]+i," "); } result.push_back(str); }else{ for(int j=0;j<mp[st].size();j++){ if(mp[st][j]==true){ sp.push_back(j+1); dfsWordBreak(s,j+1,sp,dict,result,mp); sp.pop_back(); } } }}vector<string> wordBreak(string s, unordered_set<string> &dict) { vector<string> result; vector<vector<bool>> mp(s.size(),vector<bool>(s.size(),false)); for(int i=0;i<s.size();i++){ for(int j=i;j<s.size();j++){ if(dict.find(s.substr(i,j-i+1))!=dict.end()) mp[i][j] = true; } } bool flag = false; for(int i=0;i<s.size();i++){ if(mp[i][s.size()-1]) {flag = true; break;} } if(!flag) return result; vector<int>sp; dfsWordBreak(s,0,sp,dict,result,mp); return result;}};
Java
public class Solution { List<String> result;List<Integer> solu;boolean [][] mp;public List<String> wordBreak(String s, Set<String> dict) { result = new ArrayList<>(); solu = new ArrayList<>(); mp = new boolean[s.length()][s.length()]; for(int i=0;i<s.length();i++){ for(int j=i;j<s.length();j++){ if(dict.contains(s.substring(i, j+1))) mp[i][j] = true; } } boolean flag = false; for(int i=0;i<s.length();i++){ if(mp[i][s.length()-1]){ flag = true; break; } } if(!flag) return result; dfs(s, 0, dict); return result; }public void dfs(String s, int st, Set<String> dict ){if(st>=s.length()){StringBuffer str = new StringBuffer(s);for(int i=0;i<solu.size()-1;i++){str.insert(solu.get(i)+i, " ");}result.add(str.toString());}else {for(int j=0;j<mp[st].length;j++){if(mp[st][j]==true){solu.add(j+1);dfs(s, j+1, dict);solu.remove(solu.size()-1);}}}}}
0 0
- [LeetCode]Word BreakII
- LeetCode:word-breakII
- [Leetcode] Word Break、Word BreakII
- [leetcode-140]word breakII(java)
- LeetCode OJ --问题与解答 Word BreakII
- leetcode 140 —— Word BreakII
- Leetcode——139 Word Break && 140 Word BreakII
- 每天一道算法题(七)Leetcode – Word BreakII (Java)
- 动态规划——word-break&&word-breakii
- leetCode_Word BreakII
- leetcode Word Break & Word Break ||
- LeetCode: Word Search
- [Leetcode] Word Search
- LeetCode : Word Search
- Leetcode: Word Search
- [LeetCode] Word Search
- leetcode 56: Word Search
- [Leetcode] Word Ladder
- (十六)模板与泛型编程
- .NET里简易实现AOP
- ARC中__block关键字修饰的local变量在Block中不会被强引用
- 代码编写艺术之硬编码:系统时间是硬编码
- 项目设计之---------- 模版模式利用
- [LeetCode]Word BreakII
- 山东金宇集团与用友建筑再续前缘
- UVA11090 Going in Cycle!! (二分+SPFA判断有无负权)
- mysql中char与varchar的区别分析
- 程序员生存定律--升华成高手的可能方法
- 用友NC6财务共享服务助企业财务管理转型升级
- IOS 图片自动旋转
- CRUD 含义
- 用友优普U9:聚焦离散制造,领航多组织管理