Leetcode: Concatenated Words

Posted neverlandly

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:

Input:[""]
Output:[""]
Expected:[]
如果不加21行那句,就会直接return dp[0], true了
 1 public class Solution {
 2     public List<String> findAllConcatenatedWordsInADict(String[] words) {
 3         List<String> res = new ArrayList<>();
 4         if (words==null || words.length==0) return res;
 5         HashSet<String> dict = new HashSet<>();
 6         Arrays.sort(words, new Comparator<String>() {
 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<String> 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<i; j++) {
26                 if (!dp[j]) continue;
27                 String str = word.substring(j, i);
28                 if (dp[j] && dict.contains(str)) {
29                     dp[i] = true;
30                     break;
31                 }
32             }
33         }
34         return dp[word.length()];
35     }
36 }

 

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

 

以上是关于Leetcode: Concatenated Words的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode: Concatenated Words

[LeetCode] Concatenated Words 连接单词

leetcode472. Concatenated Words

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

Leetcode 320: Generalized Abbreviation

CodeForces D. Concatenated Multiples