Last active
May 8, 2025 16:21
-
-
Save Strelok78/605341dcd8096cc4f62b7c8828c0110f to your computer and use it in GitHub Desktop.
Check open and close brackets correctness and max nesting depth
This file contains hidden or 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
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