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 ...
随机推荐
- 安装arcgis server 10.2遇到的问题总结
1.创建管理站点失败 错误提示:Failed to create the site. The machine does not have a valid license. Please authori ...
- .net 配置文件 分析 EntityName 时出错
今天用C#读写XML文档,总出现下面的错误: 分析 EntityName 时出错.行1,位置9. 出错地方的源程序为: //...... pathEle.InnerXml = reducedStr(v ...
- 谈谈.NET程序集(一)
谈谈.NET程序集(一) The Assembly in .NET by 唐小崇 http://www.cnblogs.com/tangchong 在.NET出现之前, Windows的程序有一些非常 ...
- MacOSX64位机器上gcc编译32位x264静态库
x264最新包地址:http://www.videolan.org/developers/x264.html 编译命令: ./configure --enable-static --host=i386 ...
- var, object, dynamic的区别以及dynamic的使用
var, object, dynamic的区别以及dynamic的使用 理解C# 4 dynamic(1) - var, object, dynamic的区别以及dynamic的使用 2013-06- ...
- Android开发华为手机无法看log日志解决方法
Android开发华为手机无法看log日志解决方法 上班的时候,由于开发工具由Eclipse改成Android Studio后,原本的华为手机突然无法查看崩溃日志了,大家都知道,若是无法查看日志要它毛 ...
- 从ICassFactory为CLSID为{17BCA6E8-A950-497E-B2F9-AF6AA475916F}的COM组件创建实例失败,原因是出现以下错误:c001f011.(Microsoft.Server.manageDTS
从ICassFactory为CLSID为{17BCA6E8-A950-497E-B2F9-AF6AA475916F}的COM组件创建实例失败,原因是出现以下错误:c001f011.(Microsoft ...
- IFE-20 笔记
将字符串按符号分割成数组 var str = 'aa,43,rt,55'; var arr = str.split(/[^0-9a-zA-Z\u4e00-\u9fa5]+/) //arr = [aa ...
- Springboot 入门之Hello World
首先使用maven进行包加载和配置,但是你maven一定要配置好,maven的setting.xml文件一定要配置好,不然jar包加载不了的. <project xmlns="http ...
- 嵌入式SQL
一.包含嵌入式SQL 程序的处理过程 由预处理程序对源程序进行扫描,识别出ESQL语句 把它们转换成主语言的函数调用语句,使主语言编译程序能够识别 最后由主语言的编译程序将整个源程序编译成目标码 ...