Consider the alphabet A ="()[]{}<>"
. Think of the characters in the alphabet as opening and closing brackets of 4 different types. Let's say that a string over the alphabet A is balanced if
- it is empty -or-
- it is not empty -and- the number of opening and closing brackets of each type match -and- each opening bracket has a corresponding closing bracket of the same type to the right of it -and- the substring between each pair of corresponding brackets is balanced.
For example, the strings ""
, "()"
, "<[]>"
, "{}{}[[]]"
are balanced, but the strings "("
, "{>"
, ")("
, "<>>"
, "[(])"
are not balanced.
Task: Given a (possibly unbalanced) input string S over the alphabet A, find all possible ways to insert minimal number of brackets to make the string balanced. More precisely,
Input: any string S over the alphabet A.
Output: all possible balanced strings of the shortest length over the same alphabet containing S as a subsequence.
Some examples (a string to the left of an arrow denotes an input, the list of strings to the right of the arrow denotes the corresponding output):
""
→""
"()[]"
→"()[]"
"["
→"[]"
">"
→"<>"
"{}]"
→"[{}]", "{}[]"
"<<"
→"<><>", "<<>>"
Note that:
- The output always contains at least one string.
- The output contains only those types of brackets that occur in the input string.
- All strings in the output are of the same length.
- If an input string is balanced, then the output consists of the same single string.
Implementation requirements:
- Your implementation may be in any programming language and should be as short as possible (in terms of character count or byte count - which one is less). You may represent the source in ASCII, Unicode or any other alphabet normally used in your language. If a program in your language has a shorter standard encoding as a sequence of bytes (like x86 assembly), you may choose to claim its length based on byte count, although you may submit the program in a hexadecimal form (e.g.
7E-FF-02-...
) and/or a textual form using standard mnemonics. If browsers cannot represent characters that you used in the program, you may submit a graphical image of the source and/or a sequence of keystrokes that can be used to type it, but still may claim its length based on character count. - Any required whitespace or newline characters are included in the character count. If you want, you may submit both an "official", shortest version that is used to claim the length and a "non-official", human-readable version with additional spaces, newlines, formatting, comments, parentheses, meaningful identifier names, etc.
- The implementation may be a complete program/script, a function (along with any helper functions or definitions, if needed), a functional expression (e.g. lambda, anonymous function, code block, several built-in functions combined with a function composition operator, etc) or any similar construct available in your language.
- You may use any standard library functions that normally available in your language, but any required
import
,using
,open
or other similar constructs or fully qualified names must be included in the source unless your language imports them by default. - The program may not communicate with any Internet services to obtain the result, intermediate data, or to download some parts of the algorithm.
- If your language supports some form of compressed expressions (like
Uncompress["..."]
in Mathematica) you may use them to make the program shorter. - If your language does not support strings or you do not want to use them, you may use character or integer arrays (or other collections) instead. If your language does not support integers (e.g. lambda-calculus), you may use a suitable encoding for them (like Church numerals).
- For input and output you may use whatever mechanism you prefer: standard input/output streams, user prompt, console output, function parameters and return value, Turing machine tape, reading/writing memory blocks, etc.
- You may assume that an input is well-formed (e.g. that it does not include any characters except
()[]{}<>
) and do not need to validate it. The program behavior may be undefined if the input is not well-formed. - The output strings may be printed delimited by commas, semicolons, spaces, newlines or other delimiters normally used in your language, with or without surrounding quotation marks, or returned as an array, list, sequence, a single delimited string or any other suitable collection.
- The output strings may be returned in any order, but must not contain duplicates.
- The program execution time must be polynomial with respect to the length of its output.
<
there is only 1 solution, for<<
there are 2 solutions, for<<<
there are 5 solutions, for<<<<
there are already 14 solutions. This number increases exponential. \$\endgroup\$