Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- public class Program
- {
- public static void Main()
- {
- Console.WriteLine(HasBalancedBrackets("[[[[[[[[foobar]]]]]]]]").ToString());
- }
- private static bool HasBalancedBrackets(string input)
- {
- int count = 0;
- for (int i = input.Length - 1; i >= 0; i--)
- {
- char ch = input[i];
- if (ch == ']')
- {
- count++;
- }
- else if (ch == '[')
- {
- count--;
- }
- // even if you check to the end there won't be enough to balance
- if (i - count < 0)
- {
- Console.WriteLine("too much close");
- return false;
- }
- }
- if (count > 0)
- {
- Console.WriteLine("too much close");
- }
- else if (count < 0)
- {
- Console.WriteLine("too much open");
- }
- return count == 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement