-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathTask 09.cpp
More file actions
28 lines (24 loc) · 780 Bytes
/
Task 09.cpp
File metadata and controls
28 lines (24 loc) · 780 Bytes
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
#include<iostream>
#include<stack>
using namespace std;
void braceCount(string input)
{
stack <char> closingBraces, openingBraces;
for (int i=input.length()-1; i>-1; i--) //going backwards
{
char a = input.at(i);
if (a == '(' && !closingBraces.empty()) //if there is a closing brace, pop
closingBraces.pop();()
else if (a == '(') //no closing braces, push to openingBrace stack
openingBraces.push(a);
else if (a == ')') //push closing braces into stack
closingBraces.push(a);
}
cout<<closingBraces.size()+openingBraces.size(); //result is total number of lonely braces
}
int main()
{
string input;
cin>>input;
braceCount(input);
}