Skip to content

Instantly share code, notes, and snippets.

@Strelok78
Last active May 16, 2023 16:42
Show Gist options
  • Save Strelok78/4afa73406849d0c06ede10fa28bc66bf to your computer and use it in GitHub Desktop.
Save Strelok78/4afa73406849d0c06ede10fa28bc66bf to your computer and use it in GitHub Desktop.
A string of the characters '(' and ')' is given. It is determined whether it is a valid parenthesis expression. The maximum depth of nesting of brackets is determined.
namespace MyCode
{
internal class Program
{
static void Main(string[] args)
{
const char OpenBrace = '(';
const char CloseBrace = ')';
int countBracesLevel = 0;
int countBracesCorrectness = 0;
string enteredBraces;
enteredBraces = Console.ReadLine();
foreach (char brace in enteredBraces)
{
switch (brace)
{
case OpenBrace:
countBracesCorrectness++;
break;
case CloseBrace:
countBracesCorrectness--;
break;
default:
break;
}
if (countBracesCorrectness < 0)
{
break;
}
countBracesLevel = countBracesCorrectness > countBracesLevel ? countBracesCorrectness : countBracesLevel;
}
if (countBracesCorrectness == 0)
{
Console.WriteLine($"Line is correct!");
Console.WriteLine($"Max braces level: {countBracesLevel}");
}
else
{
Console.WriteLine("Line is incorrect!");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment