-
Notifications
You must be signed in to change notification settings - Fork 0
/
673Paranthesis.cpp
52 lines (44 loc) · 1.24 KB
/
673Paranthesis.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Vasudev Vijayaraman
// UVA 673 - Paranthesis Balance
// UVA username - vasapp
// Data Structure used - Stack
// Tricks - Pushing and popping into stacks appropriately
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main()
{
string expr; // string to read in all the characters as a string
stack<char> myStack; // stack containing characters
int count; // contains the number of lines
cin >> count; // reading in the number of lines
while (count--) { // execute until the counter is 0
getline(cin, expr); // reading in the character line by line
for (int i = 0; i < expr.length(); i++)
{
if (!myStack.empty() && expr[i] == '(' && myStack.top() == ')') // pop if it is a balanced paranthesis
{
myStack.pop();
}
else if (!myStack.empty() && expr[i] == '[' && myStack.top() == ']') // pop if it is a balanced square paranthesis
{
myStack.pop();
}
else // else push back into the stack
{
myStack.push(expr[i]);
}
}
if (myStack.empty()) // checking to see of the stack is empty
{
cout << "Yes" << endl; // Yes if the stack is empty
}
else
{
cout << "No" << endl;
}
}
return 0;
}