Skip to content

Instantly share code, notes, and snippets.

@Tangrila-BG
Created May 8, 2017 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tangrila-BG/20b76064b025a2d789963ae65bf09a28 to your computer and use it in GitHub Desktop.
Save Tangrila-BG/20b76064b025a2d789963ae65bf09a28 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Csharp.Advanced.StacksAndQueues
{
public partial class StacksAndQueues
{
public static class _07BalancedParentheses
{
public static void Solution()
{
// "{, [, (, ), ], }
var input = Console.ReadLine();
var stack = new Stack<char>();
var flag = true;
foreach (char para in input)
{
switch (para)
{
case '[':
case '(':
case '{':
stack.Push(para);
break;
case '}':
if (!stack.Any())
flag = false;
else if (stack.Pop() != '{')
flag = false;
break;
case ')':
if (!stack.Any())
flag = false;
else if (stack.Pop() != '(')
flag = false;
break;
case ']':
if (!stack.Any())
flag = false;
else if (stack.Pop() != '[')
flag = false;
break;
}
if (!flag)
break;
}
// is balanced?
Console.WriteLine(flag ? "YES" : "NO");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment