This was one of a series of challenges leading up to Brain-Flak's birthday. Find out more here.
Challenge
For this challenge your objective will be to find the very first pair of matching brackets in a fully matched string of ()[]{}<>
brackets. To borrow DJMcMayhem's definition of a fully matched string:
For the purpose of this challenge, a "bracket" is any of these characters:
()[]{}<>
.A pair of brackets is considered "matched" if the opening and closing brackets are in the right order and have no characters inside of them, such as
() []{}
Or if every subelement inside of it is also matched.
[()()()()] {<[]>} (()())
Subelements can also be nested several layers deep.
[(){<><>[()]}<>()] <[{((()))}]>
A string is considered "Fully matched" if and only if each pair of brackets has the correct opening and closing bracket in the right order.
Input
Input will consist of a single nonempty string or char array containing only the characters ()[]{}<>
, and is guaranteed to be fully matched. You may take input in any reasonable manner that corresponds with our i/o defaults.
Output
The output of your program or function will be the index of the bracket which closes the first one. Output must be either 0
or 1
indexed. Again, output may be in any reasonable manner that corresponds with our i/o defaults.
Test Cases
Input 0-indexed 1-indexed
() 1 2
(<>) 3 4
<[]{<>}> 7 8
{}{}{}{} 1 2
[[]<>[]] 7 8
This is code-golf, fewest bytes wins!