Mercury: Concatenated Words
harbor run -t elfsong/mercury-concatenated-wordsharbor download elfsong/mercury-concatenated-wordsGiven 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 (not necessarily distinct) 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 <= 10⁴
1 <= words[i].length <= 30
words[i] consists of only lowercase English letters.
All the strings of words are unique.
1 <= sum(words[i].length) <= 10⁵
Implement Solution.findAllConcatenatedWordsInADict in /app/solution.py using Python 3.12.
Keep the supplied class and method signature. The file is the only submitted artifact.
The evaluator instantiates Solution and invokes this method with the problem's inputs;
do not read standard input or write a command-line program.
class Solution(object):
def findAllConcatenatedWordsInADict(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
The standard library, lctk 0.0.6 (LeetCode tree/list helpers), sortedcontainers 2.4.0, NumPy 2.4.3, SciPy 1.17.1, and six 1.17.0 are available. Standard LeetCode imports, including List, Optional, TreeNode and ListNode, are provided by the starter file.
Your solution must pass every hidden test. For a correct solution, the evaluator also measures CPU runtime over 128 repetitions per test using independent copies of the inputs, and compares it with passing reference solutions measured in the same environment. Aim for an efficient algorithm. Wrong solutions receive zero efficiency credit.