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".


题解:动态规划

设置数组dp,dp[i,j]表示从s的第i个字母(i=0~s.length-1)开始,长度为j的子字符串是否在字典dict中。则,有以下递推式:

  1. 如果字典包含子字符串s[i,i+j-1],则 dp[i,j] = true;
  2. 如果字典包含子字符串s[i,i+k-1]和字符串s[i+k,i+j],则dp[i,j] = true;
  3. 否则,dp[i,j] = false;

代码如下:

public class Solution {
public boolean wordBreak(String s, Set<String> dict) {
if(s == null || dict.size() == 0)
return false;
int length = s.length();
boolean[][] dp = new boolean[length][length+1]; for(int len = 1;len <= length;len++){
for(int i = 0;i+len <= length;i++){
String sub = s.substring(i,i+len);
if(dict.contains(sub)){
dp[i][len] = true;
continue;
} for(int k = 1;k < len;k++){
if(dp[i][k] && dp[i+k][len-k] )
{
dp[i][len] = true;
break;
}
}
}
} return dp[0][length];
}
}

对于字符串,substring这个函数返回的是beginIndex和endIndex-1之间的子字符串!

【leetcode】Word Break的更多相关文章

  1. 【LeetCode】Word Break 解题报告

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  2. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  3. 【leetcode】Word Break II

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  4. 【leetcode】Word Break(python)

    思路是这种.我们从第一个字符開始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到.那么就是False了. 在找到第一个后,接下来找下一个断句处,当然是从第一个断句 ...

  5. 【leetcode】Word Break II (hard)★

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  6. 【Leetcode】【Medium】Word Break

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  7. 【leetcode】Word Ladder

    Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...

  8. 【leetcode】Word Ladder II

      Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...

  9. 【leetcode】Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

随机推荐

  1. Android文章收藏

     Android集 1.Himi李华明的<Android游戏开发专栏>http://blog.csdn.net/column/details/androidgame.html2.老罗的&l ...

  2. 获取 Android 版本

    var a = /Android(.+?);/.exec(window.navigator.userAgent) if (a) { this.AndroidVersion = +(a[1].trim( ...

  3. WPF的TextBox抛出InvalidOperationException异常:Cannot close undo unit because no opened unit exists.

    近期遇到一个问题.应用使用过程中突然崩溃,查看dump发现异常信息例如以下: UI dispatcher has encountered a problem: 无法关闭撤消单元.由于不存在已打开的单元 ...

  4. 如何开启Apache Rewrite功能

    一.Ubuntu默认未开启Rewrite支持 apche模块加载工作已分散到不同的配置文件,这样看起来似乎更为合理,管理起来也非常方便.下面看一下如何开启Rewrite模块,当用户需使用301重定向. ...

  5. xshell 连接腾讯服务器

    1.先关机, 创建秘钥,再绑定主机,下载秘钥保存下来 2. 填写好主机好和端口 3 4.导入刚才下载的文件 记住用户名是ubuntu 不是root!!

  6. asp.net core使用中间件美化开发环境异常页面

    asp.net core系统自带的异常页面色彩给人感觉模糊.朦胧,晕眩! 原版: 美化版 实现思路:(在系统自带异常中间件“DeveloperExceptionPageMiddleware”执行后,调 ...

  7. 由浅到深理解ROS(4)

    消息和消息类型 节点能相互传递消息,节点之间通信也是基于消息.消息类型也就是数据类型数据类型,理解消息的类型很重要,因为它决定了消息的内容.也就是说,一个话题的消息类型能告诉你该话题中每个消息携带了哪 ...

  8. VirtualBox + CentOS 虚拟机网卡配置

    摘要: 要学好Linux,还是得自己搭建虚拟机. VirtualBox比较小巧简单,容易上手.在配合CentOS 6.4使用时,首要的问题就是网卡配置,尤其是使用SSH终端仿真程序(例如SecureC ...

  9. Tomcat startup.bat启动隐藏弹出的信息窗口

    to make tomcat to use javaw.exe instead of java.exe using some startup parameter or environment vari ...

  10. 微信小程序的文件结构 —— 微信小程序教程系列(1)

    所有文章均是CSDN博客所看,已按照作者要求,注明出处了,感谢作者的整理! 博客文章地址:http://blog.csdn.net/michael_ouyang/article/details/548 ...