Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.
Return all such possible sentences.
For example, given
s =”catsanddog”,
dict =[“cat”, “cats”, “and”, “sand”, “dog”].
A solution is[“cats and dog”, “cat sand dog”].

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class Solution {public  ArrayList<String> wordBreak(String s, Set<String> dict) {ArrayList<String> lists = new ArrayList<String>();ArrayList<String> strings = new ArrayList<String>();int len = s.length();dfs(s,len,dict,lists,strings);return lists;}public  void dfs(String s,int len,Set<String> dict, ArrayList<String> lists,ArrayList<String> strings) {if(len<=0){int size = strings.size()-1;StringBuilder sb = new StringBuilder();for(;size>=0;size--){sb.append(strings.get(size));if(size!=0)sb.append(" ");}     lists.add(sb.toString());}for(int k = len-1;k>=0;k--){if(dict.contains(s.substring(k,len))){strings.add(s.substring(k,len));dfs(s,k,dict,lists,strings);strings.remove(strings.size()-1);}}}
}

word-break Java code相关推荐

  1. leetcode word break java,Word Break leetcode java

    题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...

  2. Leetcode word break I II 词句拆分I和II的java实现及解析

    Leetcode word break I & II 词句拆分I和II的java实现及解析 word break i是leetcode 里面中等难度的题目而word break II 更是ha ...

  3. Java Code Convention Rules

    2019独角兽企业重金招聘Python工程师标准>>> Java Code Convention Rules Rules available in this category: Av ...

  4. LeetCode——Word Break

    LeetCode--Word Break Question Given a non-empty string s and a dictionary wordDict containing a list ...

  5. word插入Java代码

    word插入Java代码 Syntax Highlight Code in Word Documents 选择Java生成高亮代码,复制粘贴到word.

  6. LeetCode Word Break II

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

  7. Word Break Word Break II

    leetcode上面的两道动态规划题. 题目1 Word Break Given a string s and a dictionary of words dict, determine if s c ...

  8. aspose.word 20 java 替换占位符为文字或图片

    以前写过用asposeword 替换文字和图片,但是后来遇到问题,有些word替换替换图片后会导致打开时弹出错误:此文件中检测到错误,单word可以通过进行一下修复来打开文件. 考虑可能是版本有bug ...

  9. 【Leetcode】139. 拆分词句(Word Break)

    Leetcode - 139 Word Break (Medium) 题目描述:给定一个字符串 s 与字典 wordDict,判断 s 是否能拆分成 wordDict 中的子字符串. Input: s ...

  10. SQL to Elasticsearch java code

    把Elasticsearch当成Database用,因为Elasticsearch不支持SQL,就需要把SQL转换成代码实现. 1.按某个field group by查询count SELECT fi ...

最新文章

  1. 使用SQLServer配置管理器配置SQLServer数据库引擎实例,以便侦听特定的固定1433端口。...
  2. [mysql] 常用命令二
  3. 第五周项目一-三角形类雏形(4)
  4. 文档注释快捷键_自学PS:图像添加注释有什么用?如何显示或隐藏额外内容?...
  5. 这个用Java写的开源音乐播放器,我粉了
  6. 温州大学《深度学习》课程课件(六、优化算法)
  7. matlab 转换为正整数_【MATLAB】专题1笔记 MATLAB基础知识
  8. 平衡二叉树、二叉排序树-数据结构
  9. 新物联网时代的整合战略
  10. 分析CVPR 2019论文关键词,我看到了计算机视觉的最新趋势 | 附代码
  11. libiec61850探究【1】-第一个MMS通讯实例
  12. 华为云域名转入到阿里云的基本流程
  13. 数据库小型教务系统,成绩管理系统
  14. 【翻译】HCP: A Flexible CNN Framework for Multi-Label Image Classification
  15. js正则只能匹配正整数或零
  16. K均值(kmeans)分类
  17. 535A: Tavas and Nafas
  18. 如何将多个txt快速合并
  19. Nature子刊:基于静息态EEG功能连接模式识别精神疾病亚型
  20. FFMPEG学习----解码视频

热门文章

  1. 网络编程之IO多路复用
  2. 巧妙使用excel 实现行转列
  3. PHP开发API接口注意事项
  4. oracle数据备份无法导出空表解决办法
  5. IDEA的Database连接oceanbase
  6. A100 MIG 使用说明
  7. 微CLI工具箱-WeToolkit
  8. 解决UOS家庭版桌面图标消失,文件管理器进不去
  9. PCI设备驱动和相关知识
  10. 域文件服务器可不可以多个,多个域控服务器的搭建