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 ...
随机推荐
- 简单动态规划——三逆数的O(N^2)解法!
[算法]简单动态规划——三逆数的O(N^2)解法! 问题描述: 三逆数定义:给一个数的序列A[0,1,....N-1]),当i<j<k且A[i]>A[j]>A[k]时,称作ai ...
- Linux epoll总结
Linux epoll总结 Linux epoll epoll是Kernel 2.6后新加入的事件机制,在高并发条件下,远优于select.epoll最大的好处在于它不会随着监听fd数目的增长而降低 ...
- wget ( download the whole page from the website )
---恢复内容开始--- wget -m -e robots=off -U "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1 ...
- java自动生成略缩图
当你要做一个图库的项目时,对图片大小.像素的控制是首先需要解决的难题. 本篇文章,在前辈的经验基础上,分别对单图生成略缩图和批量生成略缩图做个小结. 一.单图生成略缩图 单图经过重新绘制,生成新的图片 ...
- aix 禁止root远程登录
Aix禁止root远程登录 aix用户扩展信息大都在/etc/security/user这个文本文件里.你可以修改: login=false 用户不能登录系统 rlogin=false 用户不能从远程 ...
- Windows系统架构
操作系统模型 大多数操作系统中,都会把应用程序和内核代码分离运行在不同的模式下.内核模式访问系统数据和硬件,应用程序运行在没有特权的模式下(用户模式),只能使用有限的API,且不能直接访问硬件.当 ...
- 【汇编】字符串处理指令 stosb、lodsb、movsw、scasb、rep
一.字符串处理指令 (1) lodsb.lodsw:把DS:SI指向的存储单元中的数据装入AL或AX,然后根据DF标志增减SI (2) stosb.stosw:把AL或AX中的数据装入ES:DI指向的 ...
- 怎么在Windows下安装Linux虚拟机
前提:①电脑有安装好VMware Workstation Pro虚拟机,没有的话点此下载并安装.②下载好CentOS-7-x86_64-DVD-1503-01镜像文件,没有点此下载密码:lomg. 1 ...
- 如何在标题栏的title前添加网站logo
之前在哪个网上看到的,今天没事给写下来了: 1.将像素大小为16*16的图片命名为 favicon.ico,(图片使用的颜色好像不能超过16色),放到网站根目录下就可以了; 2.在head标签中添加代 ...
- 办理卡尔加里大学(本科)学历认证『微信171922772』calgary学位证成绩单使馆认证University of calgary
办理卡尔加里大学(本科)学历认证『微信171922772』calgary学位证成绩单使馆认证University of calgary Q.微信:171922772办理教育部国外学历学位认证海外大学毕 ...