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:

Input:[""]
Output:[""]
Expected:[]
如果不加21行那句,就会直接return dp[0], true了
 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的更多相关文章

  1. [LeetCode] Concatenated Words 连接的单词

    Given a list of words (without duplicates), please write a program that returns all concatenated wor ...

  2. 【LeetCode】472. Concatenated Words 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  3. [LeetCode] Split Concatenated Strings 分割串联字符串

    Given a list of strings, you could concatenate these strings together into a loop, where for each st ...

  4. LeetCode Split Concatenated Strings

    原题链接在这里:https://leetcode.com/problems/split-concatenated-strings/description/ 题目: Given a list of st ...

  5. LeetCode 1239. Maximum Length of a Concatenated String with Unique Characters

    原题链接在这里:https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters ...

  6. [LeetCode] 555. Split Concatenated Strings 分割串联字符串

    Given a list of strings, you could concatenate these strings together into a loop, where for each st ...

  7. 【leetcode】472. Concatenated Words

    题目如下: Given a list of words (without duplicates), please write a program that returns all concatenat ...

  8. 【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 ...

  9. 【LeetCode】555. Split Concatenated Strings 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

随机推荐

  1. eclipse调试solr

    eclipse调试solr 现在solr的源码包,我这里是4.10.2, 编译, ant ivy-bootstrap ant eclipse 导入elipse,将solr/example/solr/下 ...

  2. WSDL项目---处理消息

    有几个视图选择在处理SOAP请求和响应消息. 让我们看看这两个. 请求消息 XML ——标准的底层XML消息的文本视图Validate选项验证当前的消息发现的错误:  (这里行号一直在编辑器中打开) ...

  3. 西门子成立next47部门,斥资十亿欧元投资VR/AR等初创公司

       近日,西门子公司在慕尼黑举行的"西门子创新日"现场,宣布了三个关于"创新"的新动作.首先,超过六成员工的创新应用得到肯定,其中有 25 个项目获得总数高达 ...

  4. NOIP2014 uoj20解方程 数论(同余)

    又是数论题 Q&A Q:你TM做数论上瘾了吗 A:没办法我数论太差了,得多练(shui)啊 题意 题目描述 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, ...

  5. 2016 ECJTU - STL

    1.ECJTU-STL重挂 STL 2.总结:学长出的题,本来还想ak的,结果又被虐了... 3.标程和数据:http://pan.baidu.com/s/1qYzXY2K 01    水 02  水 ...

  6. FLEX各种特效集合

    http://www.noupe.com/adobe/flex-developers-toolbox-free-components-themes-and-tutorials.html经典中的经典 h ...

  7. python 安装nltk,使用(英文分词处理,词干化等)(Green VPN)

    安装pip命令之后: sudo pip install -U pyyaml nltk import nltk nltk.download() 等待ing 目前访问不了,故使用Green VPN htt ...

  8. linux软件包管理(下)

    在vi配置文件的编写的时候我们发现#并不能注释掉一行的信息 那什么才是linux标准的注释信息呢 查看软件对应的软件包命令 rpm –ap| grep vim 大多example是需要的事例文档  双 ...

  9. ubuntu实现ramdisk

    1. linux内核提供了16个ramdisk供使用者使用,只需格式化,并挂在便可以使用.查看 ls /dev/ram* 2. 修改配置文件: sudo gedit /etc/default/grub ...

  10. Ubuntu jdk安装

    1. 创建目录 sudo mkdir /usr/lib/jvm 2. 解压 sudo tar -zxvf jdk-7u60-linux-x64.gz -C /usr/lib/jvm 3. 修改环境变量 ...