Created
May 29, 2017 13:52
-
-
Save Tangrila-BG/adde070a7a6d094e90ca3b8221aa72b1 to your computer and use it in GitHub Desktop.
This file contains 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
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() || stack.Pop() != '{') | |
flag = false; | |
break; | |
case ')': | |
if (!stack.Any() || stack.Pop() != '(') | |
flag = false; | |
break; | |
case ']': | |
if (!stack.Any() || 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