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. mongoDB 使用总结

    http://blog.chinaunix.net/space.php?uid=22270773&do=blog&id=19935171. 如果无特别用途,建议:表名,字段名 全部用小 ...

  2. Android下 调用原生相机拍照摄像

    1 http://www.cnblogs.com/franksunny/archive/2011/11/17/2252926.html 2 http://www.cnblogs.com/vir56k/ ...

  3. 了解.net mvc实现原理ActionResult/View

    了解.net mvc实现原理ActionResult/View 上一篇了解了请求至Controller的Action过程,这篇继续看源码处理Action收到请求数据再返回ActionResult到Vi ...

  4. float数据在内存中的存储方法

    浮点型变量在计算机内存中占用4字节(Byte),即32-bit.遵循IEEE-754格式标准.一个浮点数由2部分组成:底数m 和 指数e.                          ±mant ...

  5. 转:HTTP ---HTTP头的编码问题(Content-Disposition)

    最近在做项目时遇到了一个 case :需要实现一个强制在浏览器中的下载功能(即强制让浏览器弹出下载对话框),并且文件名必须保持和用户之前上传时相同(可能包含非 ASCII 字符). 前一个需求很容易实 ...

  6. 在Linux平台使用VNC连接树莓派

    你的Linux发行版本号可能已经包括了Remote Desktop Viewer(远程桌面)程序.你能够通过这个程序使用VNC连接你的树莓派.通常能够在Applications/Internet菜单以 ...

  7. 利用nginx搭建tomcat集群

    1.tomcat集群 利用nginx对请求进行分流,将请求平均的分给不同的tomcat去处理,减少单个tomcat的负载量,提高tomcat的响应速度. 2.创建多个tomcat服务器(同一个服务器上 ...

  8. Maven的pom文件内容详细理解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  9. JSTL JSP页面推断某个cookie的值和读取值....

    <c:if test="${cookie['woshop'].value eq '1'}">                 <div>           ...

  10. [译]GLUT教程 - 重整子窗体

    Lighthouse3d.com >> GLUT Tutorial >> Subwindows >> Reshape Subwindows 重整函数的回调需要处理两 ...