472. Concatenated Words (H)

Given an array of strings words (without duplicates), return all the 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 1:

Input: words = ["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".

Example 2:

Input: words = ["cat","dog","catdog"]
Output: ["catdog"]

Constraints:

  • 1 <= words.length <= 104

  • 0 <= words[i].length <= 30

  • words[i] consists of only lowercase English letters.

  • 0 <= sum(words[i].length) <= 105

Solution:

Version 1: DP:

DP求解 对于words中的每个单词w,定义一个数组dpn+1,如果dpi == true,则表示w.substr(0, i)可以由words中的已有单词连接而成。 那么状态转移方程就是:dpi = || {dpj && w.substr(j + 1, i - j) is in words},其中j < i。 最终检查dpn是否为true,如果是则将其加入结果集中。 为了加速对words中的单词的查找,用一个哈希表来保存各个单词。 这样时间复杂度可以降低到O(n * m^2),其中n是words中的单词的个数,m是每个单词的平均长度(或者最大长度)。

Version 2 DFS:

利用HashSet记录已知单词, 以及能使用已知单词组合而成的新单词.

利用recursion检查当前单词可否用已知单词组合而成.

Last updated

Was this helpful?