Skip to content

Instantly share code, notes, and snippets.

@unilecs
Created February 28, 2019 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unilecs/f034cca4b12f9f52e8c996b1250bda39 to your computer and use it in GitHub Desktop.
Save unilecs/f034cca4b12f9f52e8c996b1250bda39 to your computer and use it in GitHub Desktop.
Задача: Баланс скобок - 2
using System;
public class Program
{
private static int GetDiffBalanceBrackets(string str)
{
int balance = 0;
int tempCount = 0;
foreach (char bracket in str)
{
balance += bracket == '(' ? 1 : -1;
if (balance < 0)
{
tempCount++;
balance = 0;
}
}
return balance + tempCount;
}
public static void Main()
{
Console.WriteLine("UniLecs");
string str = ""; // 0
Console.WriteLine(string.Format("Answer = {0}", GetDiffBalanceBrackets(str)));
str = "()"; // 0
Console.WriteLine(string.Format("Answer = {0}", GetDiffBalanceBrackets(str)));
str = "(()(()"; // +2
Console.WriteLine(string.Format("Answer = {0}", GetDiffBalanceBrackets(str)));
str = "())())(("; // +4
Console.WriteLine(string.Format("Answer = {0}", GetDiffBalanceBrackets(str)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment