You are given a string s
representing a list of words. Each letter in the word has 1 or more options.
"{a,b,c}"
represents options ["a", "b", "c"]
.For example, if s = "{a,b,c}d{e,f}"
then the corresponding options are ["a", "b", "c"]
for the first part, and ["e", "f"]
for the second part.
You are to expand the string into a list of words. The order of each word must be the same as in the original string. Return the list of all possible expanded words, sorted lexicographically.
Example 1:
Input: s = "{a,b}c{d,e}f" Output: ["acdf","acef","bcdf","bcef"]
Example 2:
Input: s = "abcd" Output: ["abcd"]
Constraints:
1 <= s.length <= 50
s
consists of curly brackets '{}'
, comma ','
, and lowercase English letters.s
is guaranteed to be valid such that:
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The problem asks us to expand a string containing brace-enclosed options. The brute force approach generates every single possible combination by systematically exploring all choices at each brace group. This guarantees we find all valid expansions.
Here's how the algorithm would work step-by-step:
def brace_expansion_brute_force(expression):
possible_expansions = [expression]
while True:
new_possible_expansions = []
found_brace = False
for current_expansion in possible_expansions:
brace_start_index = current_expansion.find('{')
if brace_start_index != -1:
found_brace = True
brace_end_index = current_expansion.find('}', brace_start_index)
options_string = current_expansion[brace_start_index + 1:brace_end_index]
options = options_string.split(',')
# Iterate through options to create new strings
for option in options:
new_expansion = current_expansion[:brace_start_index] + option + current_expansion[brace_end_index + 1:]
new_possible_expansions.append(new_expansion)
else:
new_possible_expansions.append(current_expansion)
possible_expansions = new_possible_expansions
# If no braces were found, we're done
if not found_brace:
break
possible_expansions.sort()
return possible_expansions
The best way to expand braces is to break down the problem into smaller, manageable parts. We'll use a technique called recursion to handle nested brace expansions, and build the final result step-by-step.
Here's how the algorithm would work step-by-step:
def brace_expansion(expression):
# Find the first occurrence of an opening brace.
start_brace_index = expression.find('{')
if start_brace_index == -1:
return [expression]
# Find the matching closing brace.
brace_level = 0
end_brace_index = -1
for index, char in enumerate(expression):
if char == '{':
brace_level += 1
elif char == '}':
brace_level -= 1
if brace_level == 0:
end_brace_index = index
break
# Extract the content inside the braces.
options_string = expression[start_brace_index + 1:end_brace_index]
# Split the options by comma.
options = options_string.split(',')
prefix = expression[:start_brace_index]
suffix = expression[end_brace_index + 1:]
expanded_options = []
# Recursively expand each option.
for option in options:
expanded_prefix = brace_expansion(prefix)
expanded_suffix = brace_expansion(suffix)
# Combine all possibilities.
for pre in expanded_prefix:
for suf in expanded_suffix:
expanded_options.append(pre + option + suf)
return sorted(expanded_options)
Case | How to Handle |
---|---|
Null or empty input string | Return an empty list immediately as there's nothing to expand. |
Input string with no braces | Return a list containing the input string itself, treating it as a single literal. |
Input string with nested braces | Handle nested braces recursively or iteratively, ensuring correct order of expansion. |
Input string with empty brace sets e.g., '{a,}' | Treat an empty brace set as a valid option contributing to the possible expansions. |
Input string with consecutive brace sets e.g., '{a,b}{c,d}' | Expand each brace set independently and then concatenate results. |
Input string with duplicate options within a brace set e.g., '{a,a,b}' | Treat duplicate options as distinct and expand accordingly, resulting in potentially duplicate words in the output, which are then sorted and made unique. |
Input string with leading or trailing spaces | Trim the input string before processing to avoid unexpected characters affecting expansion. |
Input string with extremely deep nesting leading to a combinatorial explosion | The exponential complexity is inherent to the problem, but it's important to test for stack overflow issues with deep recursion and consider iterative approaches for potentially large inputs. |