word-break Java code
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相关推荐
- 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 ...
- Leetcode word break I II 词句拆分I和II的java实现及解析
Leetcode word break I & II 词句拆分I和II的java实现及解析 word break i是leetcode 里面中等难度的题目而word break II 更是ha ...
- Java Code Convention Rules
2019独角兽企业重金招聘Python工程师标准>>> Java Code Convention Rules Rules available in this category: Av ...
- LeetCode——Word Break
LeetCode--Word Break Question Given a non-empty string s and a dictionary wordDict containing a list ...
- word插入Java代码
word插入Java代码 Syntax Highlight Code in Word Documents 选择Java生成高亮代码,复制粘贴到word.
- LeetCode Word Break II
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- Word Break Word Break II
leetcode上面的两道动态规划题. 题目1 Word Break Given a string s and a dictionary of words dict, determine if s c ...
- aspose.word 20 java 替换占位符为文字或图片
以前写过用asposeword 替换文字和图片,但是后来遇到问题,有些word替换替换图片后会导致打开时弹出错误:此文件中检测到错误,单word可以通过进行一下修复来打开文件. 考虑可能是版本有bug ...
- 【Leetcode】139. 拆分词句(Word Break)
Leetcode - 139 Word Break (Medium) 题目描述:给定一个字符串 s 与字典 wordDict,判断 s 是否能拆分成 wordDict 中的子字符串. Input: s ...
- SQL to Elasticsearch java code
把Elasticsearch当成Database用,因为Elasticsearch不支持SQL,就需要把SQL转换成代码实现. 1.按某个field group by查询count SELECT fi ...
最新文章
- 低学历学什么技术有前途可以月入过万?
- Vue + ElementUI 如何优雅的上传文件到七牛OSS
- python网页优化_python大佬养成计划----JavaScript对html的优化
- 【算法精讲】集成分类与随机森林
- JS中双引号单引号,转义字符问题!!
- 体质测试数据统计软件,[体质测试数据excel自动统计表]体质测试数据Excel自动统计模板的研制...
- 深入解析AsyncTask
- RobotStudio机器人运行路径的创建与仿真
- python excel案例导入jira_用Python脚本批量添加JIRA用户,python,jira
- 【Java】用JavaFx实现记事万年历(实验课题目)
- Android校准指南针,电子罗盘的工作原理及校准 电子罗盘,电子指南针,android.docx...
- java搭建rtmp服务器,利用docker搭建RTMP直播流服务器实现直播
- DBSCAN密度聚类算法
- Alcor(安国)AU6387量产修复(u盘修复)
- java html转word!
- JavaScript 学习手册二:JS 数据类型
- 量产HLW8032串口通讯芯片的三相电参数采集系统项目资料
- 马斯克、脑机交互与人机融合
- 【kali-漏洞扫描】(2.1)Nessus下载安装(上)
- mysql data masking_Dynamic Data Masking 动态数据脱敏