Longest Word That Swallows Another Word in the List
StringsAuto-graded · 25 test casesLast updated
Find the longest distinct word in a list that contains a different word from the list as a contiguous substring, with a fixed tie-break and empty-string handling.
You are given a list of strings words. Find a word that has some other word from the same list inside it as a contiguous substring.
Rules
- Compare values, not positions. Duplicate entries collapse into one word, so a word never counts as containing "another" word just because it appears twice. For example,
["ab", "ab"]has no qualifying word. - A word qualifies if some different word (a different string value) in the list occurs inside it as a contiguous substring.
- The empty string
""is a substring of every string. If""is in the list, every non-empty word qualifies because of it. - If several words qualify, return the longest one. If several qualifying words share that longest length, return the lexicographically largest of them (standard Python string comparison).
- If no word qualifies, return the empty string
"".
Example of the rules
With ["programming", "am", "pro"], the word "programming" contains both "am" and "pro". It is the only qualifying word, so it is returned.
Constraints
0 <= len(words) <= 2000- Each word may be empty, and each has length at most 200.
- Words may contain any characters.
- The list may contain duplicates.
- Return
""when the list is empty or no word qualifies.
Examples
Example 1
Input: find_enclosing_word(['programming', 'am', 'pro'])
Output: 'programming'
Hints
Hint 1
If two distinct strings are such that one contains the other, which of the two must be longer? What does that say about which pairs you actually need to test?
Hint 2
Remove duplicates first, then order the words by length. A word can only contain words that are strictly shorter, so consider containers from longest to shortest.
Hint 3
To get the required tie-break, make sure that among words of equal length you try the lexicographically larger one first. Then the first container you find that holds some shorter word is the answer.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
- Deduplicate with a set. The rules compare values, so equal strings are the same word.
- Sort the unique words lexicographically, then stably by length. Words are now ordered by length, and equal-length words are in ascending lexicographic order.
- Scan containers from the end of that order, i.e. longest first, and among equal lengths the lexicographically largest first.
- For each container, look through the words that are strictly shorter. If any of them is a substring of the container, return the container.
- If no container qualifies, return
"".
Why it is correct
- Two distinct strings can only be in a containment relation if the contained one is strictly shorter. Equal-length distinct strings can never contain each other. So checking only strictly shorter candidates loses nothing and never counts a word against itself.
- Containers are visited in exactly the order the tie-break asks for (longest first, then lexicographically largest). The first one that qualifies is therefore the required answer.
- The empty string needs no special case. It is strictly shorter than any non-empty word and
"" in sis true, so any non-empty word qualifies when""is present. If the list holds only empty strings, they collapse to one word with no shorter candidate, so the result is"".
Complexity
- Time: sorting is
O(n log n)comparisons. The pair checks takeO(n² · L)for typical substring search, wherenis the number of unique words andLis the maximum length. The early exit often makes this much faster, because long words are the most likely containers. - Space:
O(n)for the deduplicated and sorted lists.
For much larger inputs, the check can be done in time linear in the total input size with an Aho-Corasick automaton built over all the words. Given the constraints here, the sorted pairwise check is simple and fast enough.
Reference solution
from typing import List
def find_enclosing_word(words: List[str]) -> str:
unique = sorted(set(words))
by_length = sorted(unique, key=len)
for container in reversed(by_length):
for candidate in by_length:
if len(candidate) < len(container) and candidate in container:
return container
return ""