The Missing MatchThe Missing Match
code-golfstringbalanced-string
You are given a string of unknown length that contains a varied amount of (
, {
, [
and their respective closing parentheses (in this context all referred to as braces).
With one exception, all the braces are all balanced, meaning an opening one has a corresponding closing one somewhere afterwards in the string.
The braces may not be "interpolated" with each other, i.e. opening brace b which is inside opening brace a must close before brace a closes, so ([)]
, {[(]})
, and ({[(])}())[()]
are invalid, but ([])
, {[()]}
, and ({[()])}()[()]
are valid.
Challenge
The task is, inside the string there is a single unbalanced brace, i.e. an opening brace that does not have a closing one. You must insert the closing brace where it is the farthest distance apart from the opening brace, but not interpolated with other braces, then return the modified (or new) string.
Test cases
^
added to point out where a brace was added.
"(" --> "()"
^
"[(]" --> "[()]"
^
"{[({}()([[[{}]]])]{[]}}" --> "{[({}()([[[{}]]]))]{[]}}"
^
"[[[]]" --> "[[[]]]"
^
"{[[]([(()){[[{}]()]})]}" --> "{[[]([(()){[[{}]()]}])]}"
^
Scoring
This is code-golf, so shortest code wins.
Meta
- Is my definition of "brace interpolation" easy to understand and clear?
- Is there something in the challenge that I didn't consider?
- Is this too complicated? If so how can I dumb it down?