-
-
Save unilecs/f034cca4b12f9f52e8c996b1250bda39 to your computer and use it in GitHub Desktop.
Задача: Баланс скобок - 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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