Created
May 8, 2017 20:27
-
-
Save Tangrila-BG/20b76064b025a2d789963ae65bf09a28 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()) | |
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