Coding problem
Balanced brackets
The task
A config editor checks each template before saving it, and a template whose brackets do not match is refused.
Complete BracketsBalanced(string text). The text holds only the characters ( ) [ ] { }. Return true if every opening bracket is closed by a bracket of the same kind and the brackets close in the right order: the last one opened is the first one closed. An empty text is balanced.
Signature
public static bool BracketsBalanced(string text)
Examples
"([]{})"→true. [] and {} open and close inside the ( )."[(])"→false. ( was opened last, but ] tries to close first.
Limits: Up to 20,000 characters, nested up to 10,000 deep.
The tests check: an empty text, an opening or a closing bracket alone (with no error), openers left over, an extra closer, a closer before its opener, crossed and wrong kinds, and 10,000 nested pairs.
Hints
The solution opens by itself when all tests pass. You remember more if you try first.
Sign in to see the solutionYour code
Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;
public static class Solution
{
// Returns true when every bracket is closed by the same kind, in the right order.
public static bool BracketsBalanced(string text)
{
throw new NotImplementedException();
}
}Sign in to run and submit your code. It stays here while you do.