Skip to content

Instantly share code, notes, and snippets.

@Strelok78
Last active May 8, 2025 16:21
Show Gist options
  • Save Strelok78/605341dcd8096cc4f62b7c8828c0110f to your computer and use it in GitHub Desktop.
Save Strelok78/605341dcd8096cc4f62b7c8828c0110f to your computer and use it in GitHub Desktop.
Check open and close brackets correctness and max nesting depth
namespace CodeWarsExercises
{
class Program
{
static void Main(string[] args)
{
string symbols = "(()))))))(()))((((((";
char openBracket = '(';
char closeBracket = ')';
int maxNestingCount = 0;
int currentNestingDepth = 0;
for (int i = 0; i < symbols.Length; i++)
{
if (symbols[i] == openBracket)
{
currentNestingDepth++;
maxNestingCount = Math.Max(maxNestingCount, currentNestingDepth);
}
else if (symbols[i] == closeBracket)
{
currentNestingDepth--;
if (currentNestingDepth < 0)
{
break;
}
}
}
if (currentNestingDepth == 0)
{
Console.WriteLine($"The string is correct. The maximum nesting count is {maxNestingCount}.");
}
else
{
Console.WriteLine("The string is incorrect.");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment