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. zabbix api创建screen vsize限制解决

    通过脚本调用zabbix api 生成screen报错: "vsize": must be between "1" and "100" 查看 ...

  2. python测试网页是否能正常登陆

    #!/usr/bin/python #encoding:utf-8 ##实现网页的登陆检查 import HTMLParser import urlparse import cookielib imp ...

  3. Python内置函数之staticmethod()

    staticmethod(function)返回函数的静态方法.一般来说,实例对象调用类方法不用传入参数,因为实例对象本身隐式的作为第一个参数传入了.而采用静态方法之后,实例对象在调用类方法时必须传入 ...

  4. ifconfig 命令

    许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...

  5. codeforces 427 div.2 F. Roads in the Kingdom

    F. Roads in the Kingdom time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  6. C++编译错误C2365

    曾经我们说重定义一般是函数或者变量的重定义.今天遇到了一个新类型的重定义errorC2365 #include <iostream> using namespace std; class ...

  7. NYOJ 492 King (状态压缩)

    做题感悟:做完这题发现状态压缩有很多须要优化的地方. 解题思路:状态压缩 開始自己用的一般的思路,就和炮兵阵地,郑厂长等题类似的方法做的,開始超时,然后把数组开到了最小的极限就险过.然后看了别人的代码 ...

  8. 使用Erlang实现简单的排序算法:快速排序,冒泡排序,插入排序

    [排序算法] -module(sort). -compile(export_all). %%快速排序 qsort([]) -> []; qsort([Pivot|T]) -> qsort( ...

  9. Hugo hexo 搭建博客系列1:自己的服务器

    hexo jekyll https://hexo.io/zh-cn/ http://theme-next.iissnan.com/getting-started.html Hexo 是高效的静态站点生 ...

  10. 06 nginx Location详解之精准匹配

    一:Location详解之精准匹配 location 语法 location 有”定位”的意思, 根据Uri来进行不同的定位. 在虚拟主机的配置中,是必不可少的,location可以把网站的不同部分, ...