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