Leetcode: Concatenated Words
Given a list of words, please write a program that returns all concatenated words in the given list of words. A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array. Example:
Input: ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"] Output: ["catsdogcats","dogcatsdog","ratcatdogcat"] Explanation: "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".
Note:
The number of elements of the given array will not exceed 10,000
The length sum of elements in the given array will not exceed 600,000.
All the input string will only include lower case letters.
The returned elements order does not matter.
Scan through array and DP:
We iterate through each word
and see if it can be formed by using other words
. The subproblem is Word Break I.
But it is a little different from Word Break I, because our result only want words that are concantenated by 2 or more other words in the given array.
How to tell if a word is only by itself in the given array, or it can be concatenated by other words in the given array?
The trick is: it is obvious that a word
can only be formed by words
shorter than it. So we can first sort the input by length of each word
, and only try to form one word
by using words
in front of it. We also do not add the current word to dictionary when determine if it can be concantenated.
小心一个test case:
public class Solution {
public List<String> findAllConcatenatedWordsInADict(String[] words) {
List<String> res = new ArrayList<>();
if (words==null || words.length==0) return res;
HashSet<String> dict = new HashSet<>();
Arrays.sort(words, new Comparator<String>() {
public int compare(String str1, String str2) {
return str1.length() - str2.length();
}
}); for (String word : words) {
if (canForm(word, dict))
res.add(word);
dict.add(word);
}
return res;
} public boolean canForm(String word, HashSet<String> dict) {
if (dict.isEmpty()) return false;
boolean[] dp = new boolean[word.length()+1];
dp[0] = true;
for (int i=1; i<=word.length(); i++) {
for (int j=0; j<i; j++) {
if (!dp[j]) continue;
String str = word.substring(j, i);
if (dp[j] && dict.contains(str)) {
dp[i] = true;
break;
}
}
}
return dp[word.length()];
}
}
这题还应该研究一下Trie的解法, 目前还没有想好,也没有看到不错的Trie 解法
Leetcode: Concatenated Words的更多相关文章
- [LeetCode] Concatenated Words 连接的单词
Given a list of words (without duplicates), please write a program that returns all concatenated wor ...
- 【LeetCode】472. Concatenated Words 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- [LeetCode] Split Concatenated Strings 分割串联字符串
Given a list of strings, you could concatenate these strings together into a loop, where for each st ...
- LeetCode Split Concatenated Strings
原题链接在这里:https://leetcode.com/problems/split-concatenated-strings/description/ 题目: Given a list of st ...
- LeetCode 1239. Maximum Length of a Concatenated String with Unique Characters
原题链接在这里:https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters ...
- [LeetCode] 555. Split Concatenated Strings 分割串联字符串
Given a list of strings, you could concatenate these strings together into a loop, where for each st ...
- 【leetcode】472. Concatenated Words
题目如下: Given a list of words (without duplicates), please write a program that returns all concatenat ...
- 【leetcode】1239. Maximum Length of a Concatenated String with Unique Characters
题目如下: Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have ...
- 【LeetCode】555. Split Concatenated Strings 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
随机推荐
- 【JavaScript兼容】关于IE8及以下无法通过getElementsByClassName()方法获得元素的解决方法
try{ var a = document.getElementsByClassName("cla"); console.log(a); }catch(ex){ var array ...
- Delphi 操作Flash D7~XE10都有 导入Activex控件 shockwave
http://www.cnblogs.com/devcjq/articles/2906224.html Flash是Macromedia公司出品的,用在互联网上动态的.可互动的shockwave.它的 ...
- Is It A Tree?[HDU1325][PKU1308]
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- word2013删除下载的模板
word2013删除下载的模板 删除步骤: 1)关闭相关的word文档. 2)按照以下的路径找到相应的位置:"%系统根目录%\Users\Administrator\AppData\Roam ...
- 使用Git进行项目管理
首先在https://git.oschina.net进行注册以及登陆 登陆进去之后,如果想要创建项目,可以在 点击加号按钮,进行项目创建 3.这里以创建私有项目为例: 输入完成后,点击“创建”,进入下 ...
- this用法(ryf)
this是javascript语言的一个关键字 它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用,比如: function test(){ this.x = 1; } 随着函数使用场合的不 ...
- 阿里云服务器Linux CentOS安装配置(二)yum安装svn
阿里云服务器Linux CentOS安装配置(二)yum安装svn 1.secureCRT连接服务器 2.先创建一个文件夹,用来按自己的习惯来,用来存放数据 mkdir /data 3.yum安装sv ...
- lvs源代码分析
以linux-2.6.21为例. 数据结构介绍: ip_vs_conn 对于某个连接记录其N元组, (client, vserver, rserver) & (address, port) Q ...
- css基础知识
CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明.其中选择器通常是您需要改变样式的 HTML 元素(比如p标签),也可以是节点的属性的值(比如id,class):每条声明都是一条字典key ...
- 兼容iOS 10 资料整理笔记
原文链接:http://www.jianshu.com/p/0cc7aad638d9 1.Notification(通知) 自从Notification被引入之后,苹果就不断的更新优化,但这些更新优化 ...