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. 低学历学什么技术有前途可以月入过万?
  2. Vue + ElementUI 如何优雅的上传文件到七牛OSS
  3. python网页优化_python大佬养成计划----JavaScript对html的优化
  4. 【算法精讲】集成分类与随机森林
  5. JS中双引号单引号,转义字符问题!!
  6. 体质测试数据统计软件,[体质测试数据excel自动统计表]体质测试数据Excel自动统计模板的研制...
  7. 深入解析AsyncTask
  8. RobotStudio机器人运行路径的创建与仿真
  9. python excel案例导入jira_用Python脚本批量添加JIRA用户,python,jira
  10. 【Java】用JavaFx实现记事万年历(实验课题目)
  11. Android校准指南针,电子罗盘的工作原理及校准 电子罗盘,电子指南针,android.docx...
  12. java搭建rtmp服务器,利用docker搭建RTMP直播流服务器实现直播
  13. DBSCAN密度聚类算法
  14. Alcor(安国)AU6387量产修复(u盘修复)
  15. java html转word!
  16. JavaScript 学习手册二:JS 数据类型
  17. 量产HLW8032串口通讯芯片的三相电参数采集系统项目资料
  18. 马斯克、脑机交互与人机融合
  19. 【kali-漏洞扫描】(2.1)Nessus下载安装(上)
  20. mysql data masking_Dynamic Data Masking 动态数据脱敏

热门文章

  1. 电子沙盘数字沙盘大数据可视化交互地理信息系统开发教程第8课
  2. 使用vivado封装IP
  3. Zedboard资料
  4. 程序员编程艺术:第九章、闲话链表追赶问题
  5. Linux输入绝对值 (Missing $ inserted,Missing delimiter)
  6. SAP PP模块生产版本主数据维护
  7. 任何一台计算机都可以安装win 7系统,任何电脑都能用U盘装64位win7么?
  8. lgo怎么打开o文件_3种方法来打开O文件
  9. 六问射频脉冲频谱测试
  10. LeetCode 69. x 的平方根:二分查找法实现自定义的函数:x 的平方根