博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode: Concatenated Words
阅读量:7013 次
发布时间:2019-06-28

本文共 2652 字,大约阅读时间需要 8 分钟。

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,000The 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了
1 public class Solution { 2     public List
findAllConcatenatedWordsInADict(String[] words) { 3 List
res = new ArrayList<>(); 4 if (words==null || words.length==0) return res; 5 HashSet
dict = new HashSet<>(); 6 Arrays.sort(words, new Comparator
() { 7 public int compare(String str1, String str2) { 8 return str1.length() - str2.length(); 9 }10 });11 12 for (String word : words) {13 if (canForm(word, dict))14 res.add(word);15 dict.add(word);16 }17 return res;18 }19 20 public boolean canForm(String word, HashSet
dict) {21 if (dict.isEmpty()) return false;22 boolean[] dp = new boolean[word.length()+1];23 dp[0] = true;24 for (int i=1; i<=word.length(); i++) {25 for (int j=0; j

 

这题还应该研究一下Trie的解法, 目前还没有想好,也没有看到不错的Trie 解法

 

转载地址:http://waqtl.baihongyu.com/

你可能感兴趣的文章