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 ...
随机推荐
- C#私房菜[二][提供编程效率的技巧]
AaronYang的C#私房菜[二][提供编程效率的技巧] 前言 我的文章简单易懂,能学到东西.因为复杂的东西,讲起来,好累.阅读者只是膜拜,学不到东西,就是没用的东西,好多文章都是看不下去.我写不出 ...
- Alfred工具
神兵利器——Alfred 有人的地方就有江湖,有江湖就有纷争. 很多人说我的文字风格相对轻松和温和,那是因为我很早就认识到,我们没有教育脑残和喷子的义务.在网际多年,看过太多虚拟的刀锋和鲜血,很多人被 ...
- 百度地图定位SDK 之构想
百度地图定位 前提 从香港旅游回来,心中油然升起一股热血滂湃,激励自己发现市场需求,向创业奋进,朝着梦想前进. 简介 百度Android定位SDK支持Android1.5以及以上设备,提供: 定位功能 ...
- CSS样式基础知识
CSS样式基础知识 CSS样式概述 CSS是Cascading Style Sheet 的缩写.译作“层叠样式表单”.是用于(增强)控制网页样式并允许将样式信息与网页内容分离的一种标记性语言. 引用位 ...
- C# 操作 Excel 常见问题收集和整理
C# 操作 Excel 常见问题收集和整理(定期更新,欢迎交流) 经常会有项目需要把表格导出为 Excel 文件,或者是导入一份 Excel 来操作,那么如何在 C# 中操作 Excel 文件成了一个 ...
- [每日一题] OCP1z0-047 :2013-07-16 主键与唯一索引
主键包括非空和唯一约束,它会自动创建唯一索引(注:唯一约束也会自动创建唯一索引),测试如下: 1. 创建一个表products gyj@OCM> Create table products( 2 ...
- Thinking in C++: 静态指针与指向静态类型的指针
当将const与指针一起使用的时候,一般有两种情况,const被应用到指针指向的那个东西,或者const被应用到存储在指针中的内存地址. 第一种情况:pointer to const 注意:const ...
- 图的匹配问题与最大流问题(三)——最大流问题Ford-Fulkerson方法Java实现
上篇文章主要介绍了Ford-Fulkerson方法的理论基础,本篇给出一种Java的实现. 先借助伪代码熟悉下流程 FORD-FULKERSON(G,t,s) 1 for each edge(u,v) ...
- gcc与g++的区别与联系
gcc和g++都是GNU(组织)的编译器. 一.误区详解 误区一:gcc只能编译c代码,g++只能编译c++代码 两者都可以,但是请注意: 1.后缀为.c的文件, gcc把它当作是C程序,而g++ ...
- Android开发(19)---常见dialog对话框的运用
Dialog是android开发过程中最常用到的组件之一,Dialog的创立办法有两种: 一是直接new一个Dialog目标,然后调用Dialog目标的show和dismiss办法来操控对话框的显现和 ...