Google | Interview Question | Overchoice (Backtracking) | Phone Screen
Anonymous User
1035

You are given a string s of lowercase alphabet characters, "[", "|", and "]". "[ab|c|def]" means either "ab", "c", or "def" can be chosen as a possibility. Return a list of strings containing all possible values that s can represent.

Note that "[]" won't be nested and may have any number of choices.

Examples:

Input:
s = '>[a|bc|d]__[wx|y|z]<'
Output:
['>a__wx<', '>a__y<', '>a__z<', '>bc__wx<', '>bc__y<', '>bc__z<', '>d__wx<', '>d__y<', '>d__z<']

Input:
s = 'x[a|b]y[c|d]z'
Output:
['xaycz', 'xaydz', 'xbycz', 'xbydz']

Input:
s = '[a|b]with[x|y|z]'
Output:
['awithx', 'awithy', 'awithz', 'bwithx', 'bwithy', 'bwithz']

It is back-tracking based solution. I still had a hard time doing it.

Comments (4)