Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- namespace E08_BalancedParentheses
- {
- class BalancedParentheses
- {
- static void Main(string[] args)
- {
- // 75% - 2 Wrong Tests - 4, 8
- // https://judge.softuni.bg/Contests/Practice/Index/1447#7
- string input = Console.ReadLine();
- if (input.Length % 2 != 0 || input == "")
- {
- Console.WriteLine("NO");
- }
- else
- {
- int halfStringLength = input.Length / 2;
- string firstHalf = input.Substring(0, halfStringLength);
- string secondHalf = input.Substring(halfStringLength);
- Stack<char> first = new Stack<char>(firstHalf);
- Stack<char> second = new Stack<char>(secondHalf.Reverse());
- bool isValid = true;
- for (int i = 0; i < halfStringLength; i++)
- {
- if ((first.Peek() == '(' && second.Peek() == ')')
- || (first.Peek() == '[' && second.Peek() == ']')
- || (first.Peek() == '{' && second.Peek() == '}'))
- {
- first.Pop();
- second.Pop();
- }
- else
- {
- isValid = false;
- break;
- }
- }
- if (isValid)
- {
- Console.WriteLine("YES");
- }
- else
- {
- Console.WriteLine("NO");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement