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 ...
随机推荐
- amd和CMD
AMD(Modules/Asynchronous-Definition).CMD(Common Module Definition)规范区别? Asynchronous Module Definiti ...
- 2016 Multi-University Training Contest 2
8/13 2016 Multi-University Training Contest 2官方题解 数学 A Acperience(CYD)题意: 给定一个向量,求他减去一个 α(>=0)乘以 ...
- Repeater用法
Repeater用法: 使用Repeater可以绘制表头.表内.表尾比较复杂的表格,如以下实例: <asp:Repeater ID="Repeater1" runat=&qu ...
- 【Alpha】Daily Scrum Meeting第六次
一.本次Daily Scrum Meeting主要内容 各队员的任务完成情况 接下去要做的任务有哪些方面的问题 二.项目进展 学号尾数 今日已完成任务 接下去要做 502 统一Excel表头数据的英文 ...
- MySQL中INFORMATION_SCHEMA是什么?(2)
information_schema数据库表说明: SCHEMATA:提供了当前mysql实例中所有数据库的信息.是show databases的结果取之此表. TABLES:提供了关于数据库中的表的 ...
- vs2015帮助文档
1)注释快捷键: CTRL + K - CTRL + C (注释) CTRL + K 然后 CTRL + U (取消注释) shift+"*"---------整段(取消)注释 2 ...
- 四则运算之Right-BICEP测试
Right-结果是否正确? 正确 B-是否所有的边界条件都是正确的? Conformance(一致性):值是否和预期的一致 是一致的 Ordering(顺序性):值是否如应该的那样 是 是有序或者无 ...
- java设计模式
五种创建型模式: 1.工厂模式 普通工厂模式: 工厂类提供一个方法可以生产多种实现了某种接口的类 多方法工厂模式: 一个方法对应一个要生产的类 静态工厂模式: 静态方法来生产类 2.抽象工厂模式 工厂 ...
- python计算apache总内存
#!/usr/bin/env python import os from subprocess import Popen, PIPE def getPid(): p=Popen(['pidof','h ...
- 简单的jquery tab
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...