forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_outermost_parenthesis.cpp
53 lines (52 loc) · 1.11 KB
/
remove_outermost_parenthesis.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
53
#include <bits/stdc++.h>
using namespace std;
//Main Begin
int main()
{
string str, res;
//Count variable take care
//of number of count prsent
int count = 0;
//Input the String
cout << "Enter String :" << endl;
cin >> str;
//For Each loop
//traverse the entire str
for (auto ch : str) {
/*Check if '(' is present,
then increment count variable
by +1
else decrement the count variable
by -1
*/
if (ch == '(') {
++count;
//if count >0 then
//add ch to res String
if (count != 1) {
res += ch;
}
}
else {
--count;
//if count >1 then
//add ch to res String
if (count != 0) {
res += ch;
}
}
}
//Print the final output
cout << "Output:" << res;
return 0;
}
/* Sample Input Output 1.
Enter String : "(()())(())"
Output: ()()()
Sample Input Output 1.
Enter String : "()()"
Output: ""
Complexities :
Time Complexity : O(n)
Space Complexity : O(n)
*/