Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Levy27ops/0669fba49dee839d6defb594e982886a to your computer and use it in GitHub Desktop.
Save Levy27ops/0669fba49dee839d6defb594e982886a to your computer and use it in GitHub Desktop.
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