Created
May 16, 2025 06:47
-
-
Save Levy27ops/0669fba49dee839d6defb594e982886a to your computer and use it in GitHub Desktop.
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 Nesting | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Enter your the Charecter ("); | |
string input = Console.ReadLine(); | |
int currentBalance = 0; | |
int maxDepth = 0; | |
bool isValid = true; | |
foreach (char c in input) | |
{ | |
if (c == '(') | |
{ | |
currentBalance++; | |
} | |
else | |
currentBalance--; | |
if (currentBalance < 0) | |
{ | |
isValid = false; | |
} | |
if (currentBalance > maxDepth) | |
{ | |
maxDepth = currentBalance; | |
} | |
} | |
if (currentBalance != 0) | |
{ | |
isValid = false; | |
} | |
Console.WriteLine(isValid ? "Valid" : "Invalid"); | |
Console.WriteLine($"Max depth is {maxDepth}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment