LeetCode #139. Word Break 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.
For example, given s = "leetcode", dict = ["leet", "code"].
Return true because "leetcode" can be segmented as "leet code".
Solution:
Need to use DP because derictly loop through the string may not return the correct answer.
Test case: s="aaaaaaa", dict = ["aaa", "aaaa"]
Two solutions:
First Dynamic programming:
dp[0] = true;// always true, because empty string can always add space to it.
dp[1] = dp[0]&&dict.Contains(s.Substring(0, 1);
dp[2] =( dp[1]&&dict.Contains(s.Substring(1,1))) ||(dp[0]&& dict.Contains(s.Substring(0,2)));
so need to loop through dp[j]&&wordDict.Contains(s.Substring(j,i-j)) untill dp[i] is true where j from 0 to i;
public class Solution {
public bool WordBreak(string s, ISet<string> wordDict)
{
ISet<int> set = new HashSet<int>();
if(string.IsNullOrEmpty(s))
{
return true;
}
int l = s.Length;
bool[] dp = new bool[l+];
dp[] = true;
for(int i=; i<l+; i++)
{
for(int j=; j<i; j++)
{
dp[i]=dp[j]&&wordDict.Contains(s.Substring(j,i-j));
if(dp[i])
{
break;
}
}
}
return dp[l];
}
}
Second DFS:
Use recursion in Dfs to mark the indexes those already looped through and returned false;
public class Solution {
public bool WordBreak(string s, ISet<string> wordDict)
{
ISet<int> set = new HashSet<int>();
return Dfs(s, , wordDict, set);
}
private bool Dfs(string s, int index, ISet<string> dict, ISet<int> set)
{
// base case
if (index == s.Length) return true;
// check memory
if (set.Contains(index)) return false;
// recursion
for (int i = index + ; i <= s.Length; i++)
{
String t = s.Substring(index, i-index);
if (dict.Contains(t))
if (Dfs(s, i, dict, set))
return true;
else
set.Add(i);
}
set.Add(index);
return false;
}
}
LeetCode #139. Word Break C#的更多相关文章
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- Leetcode#139 Word Break
原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...
- LeetCode 139. Word Break单词拆分 (C++)
题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...
- leetcode 139. Word Break ----- java
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- [leetcode]139. Word Break单词能否拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- [LeetCode] 139 Word Break(BFS统计层数的方法)
原题地址: https://leetcode.com/problems/word-break/description/ 题目: Given a non-empty string s and a dic ...
- [LeetCode] 139. Word Break 拆分词句
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- Java for LeetCode 139 Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
随机推荐
- python打包成window可执行程序
python程序可以通过python hello.py执行,但是需要安装python的解释器,并配置环境变量,打包成exe程序之后可以直接执行. 使用setup工具和py2exe可以做到这一点. 最简 ...
- Eclipse RCP /Plugin移除Search对话框
RCP:如何移除Search对话框中不需要的项 2013-08-18 22:31 by Binhua Liu, 231 阅读, 0 评论, 收藏, 编辑 前言 很久没写文章了,准备写一系列关于Ecli ...
- Java网络请求getInputStream异常
今天调试网络请求部分时,当getInputStream失败时直接抛出异常.解决方法时在getInputStream之前获取ResponseCode if( connection.getResponse ...
- Arduino live weather broadcasting 实时天气站
Live broadcasting with arduino get a pc , make it run linux. make arduino catch the weather sensor a ...
- hibernate4 spring3.2 事务不提交分析
最近在做微信项目,我搭建了一个基于servlet,spring3.2,hibernate4.1的框架.因为基于消息的servlet和基于业务层是分开做的,也就是先把业务层做了,再去将所有的请求转到业务 ...
- 使用Pechkin将HTML网页转换为PDF
Pechkin开源组件使用wkhtmlbox,可以解析CSS样式,将网页转换为PDF文件, 支持URL,或者HTML字符串 1, 从NuGet程序管理器中获得Pechkin GlobalConfig ...
- Lucene简介(理论篇)
Lucene 是一个软件程序的库或者说是一个工具套件,而不是一个完全的具有搜索特性的应用程序.它关注于自己的文本检索和搜索功能,提供API来完成商业中所涉及到的搜索功能.在搜索功能中,Lucene的功 ...
- SQLsever2008 远程连接错误 linq
如果你也和我一样远程连接一个sqlsever2008数据时出现类似错误 SqlException (0x80131904): 用户 ‘xxxxx' 登录失败. 首先在“服务器资源管理器”中测试一下你的 ...
- 回收站引发ORACLE查询表空间使用缓慢
一个哥们问我 ,他们查询 表空间使用率 跑了一个多小时,这个太坑爹了,让我 帮忙优化一下. SQL语句如下 select * from ( select ts.tablespace_name,ts.c ...
- UVa1003-Cutting sticks
试题描述 将一段木棒按要求切割,每次切割都要付出与木棒长度相同的代价,求最小代价切割. (多组数据) 输入描述 长度L. 切割点数n(n<=50). n个切割点. 输出描述 "The ...