A string contains plain characters and parenthesised groups. Each group like (abc)
means "choose exactly one of these characters here." Return all possible expansions, in
the natural left-to-right, in-group order.
parentheticalPossibilities(s)
Groups never nest. A plain character is fixed.
Input format: the string. Output the expansions separated by ;.
"x(ab)" // → "xa;xb"
"(ab)(cd)" // → "ac;ad;bc;bd"
"abc" // → "abc"
The string is a sequence of units read left to right: some fixed single characters, some groups offering several options. The output is every combination from picking one option per group, keeping fixed characters as-is, read in natural order.
The first unit — plain character or group — is independent of everything after it. Whatever you choose there, the rest still needs full expansion, producing its own list of suffixes. The answer is every combination of "a choice for the first unit" with "one expanded suffix."
(, the options are the characters inside, and the rest begins after the closing ). If it's a plain character, the only option is that character, and the rest begins at the next character.An empty string has exactly one expansion: the empty string. This lets the gluing work correctly all the way down — at the innermost level, prefixing the last unit's options onto the single empty-string expansion just yields the options themselves.
Since every final string is built by one choice per unit, and the recursion visits units strictly left to right trying options in order, the nested "for each option, for each expansion of the rest" naturally produces expansions in the shown left-to-right order — the first group varies slowest (each choice starts a new block), later groups vary faster within each block.
A fixed number of groups could use one nested loop per group — but the group count is only known at runtime and can vary. Recursion handles "however many groups happen to be here" without needing that count in advance: each call handles one unit and delegates the rest to itself.
def parenthetical_possibilities(s):
if len(s) == 0:
return [""]
if s[0] == "(":
end = s.index(")")
options = list(s[1:end])
rest = s[end + 1:]
else:
options = [s[0]]
rest = s[1:]
result = []
for opt in options:
for suffix in parenthetical_possibilities(rest):
result.append(opt + suffix)
return result
Sign in to join the discussion.
No comments yet. Be the first to ask a question.
Press Run to execute your code, or Submit to test and complete this problem.