Filter Words Expandable to Target String
StringsAuto-graded · 20 test casesLast updated
Determine which words from a dictionary can yield a target string by expanding consecutive duplicate characters in place.
Given a target string target_str and a list of candidate strings word_list, identify all words in word_list that can be expanded to form target_str by duplicating any of their characters zero or more times in place.
A word can expand to target_str if and only if:
- It contains the exact same sequence of character runs (consecutive identical characters) in the exact same order as
target_str. - For every corresponding run, the length of the run in the candidate word is less than or equal to the length of the run in
target_str.
Return a list of all matching words in the order they originally appeared in word_list.
Constraints
target_strcontains only lowercase English letters ('a'–'z'). Length is between 0 and $10^5$.word_listcontains unique candidate strings consisting of lowercase English letters ('a'–'z').- Total length of all strings in
word_listdoes not exceed $10^6$. - The empty word
""matchestarget_strif and only iftarget_stris also"".
Examples
Example 1
Input: find_expandable_words('heellp', ['help', 'hellp', 'heelp', 'heeellp', 'heap'])
Output: ['help', 'hellp', 'heelp']
Example 2
Input: find_expandable_words([''], [''])
Output: []
Hints
Hint 1
Break both the target string and candidate words into run-length encodings (character and run length).
Hint 2
Compare the list of runs character-by-character: characters must match, sequence length must match, and word run lengths cannot exceed target run lengths.
Hint 3
You can compress target_str once at the start to quickly evaluate each candidate word.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
When expanding characters in place, adjacent duplicate characters cannot change order, disappear, or jump across distinct character boundaries. Therefore, the essential unit of comparison is a run—a group of identical adjacent characters represented as (character, count).
- Compress the Target String: Convert
target_strinto a list of runs[(c1, count1), (c2, count2), ...]. - Compress and Validate Candidate Words: For each word in
word_list:- Compress the word into its own list of run pairs.
- A word matches if:
- It has the exact same number of runs as
target_str. - For every run index
i,word_runs[i].char == target_runs[i].char. - For every run index
i,word_runs[i].count <= target_runs[i].count(since expanding a character can only increase its count).
- It has the exact same number of runs as
Complexity Analysis
- Time Complexity: $\mathcal{O}(N + \sum |W|)$ where $N$ is the length of
target_strand $\sum |W|$ is the total length of all words inword_list. Compressingtarget_strtakes $\mathcal{O}(N)$ time. Compressing each word takes time proportional to the word's length, and comparing run lists takes time bounded by the word length. - Space Complexity: $\mathcal{O}(N + \max |W|)$ auxiliary space to store the compressed runs of
target_strand the current candidate word.
Python Reference Solution
from typing import List
def find_expandable_words(target_str: str, word_list: List[str]) -> List[str]:
# Compress target_str into consecutive run pairs: (char, count)
target_runs = []
for ch in target_str:
if target_runs and target_runs[-1][0] == ch:
target_runs[-1][1] += 1
else:
target_runs.append([ch, 1])
valid_words = []
for word in word_list:
# Compress word into runs
word_runs = []
for ch in word:
if word_runs and word_runs[-1][0] == ch:
word_runs[-1][1] += 1
else:
word_runs.append([ch, 1])
# Check matching structure and run lengths
if len(word_runs) == len(target_runs):
if all(
w_char == t_char and w_cnt <= t_cnt
for (w_char, w_cnt), (t_char, t_cnt) in zip(word_runs, target_runs)
):
valid_words.append(word)
return valid_words