Unravel all the Possibilities

Published by persolut in

Write a function that takes in a string and returns all possible combinations. Return the final result in alphabetical order as a string[].

Examples

Unravel("a[b|c]") ➞ { "ab", "ac" }

Unravel("a[b|c]de[f|g]") ➞ { "abdef", "acdef", "abdeg", "acdeg" }

Unravel("a[b]c[d]") ➞ { "abcd" }

Unravel("a[b|c|d|e]f") ➞ { "abf", "acf", "adf", "aef" }

Unravel("apple [pear|grape]") ➞ { "apple grape", "apple pear" }

Notes

Think of each element in every block (e.g. [a|b|c]) as a fork in the road.

Watch a quick demo on how Edabit works.