-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
43 lines (38 loc) · 1.07 KB
/
Copy pathsolution.cpp
File metadata and controls
43 lines (38 loc) · 1.07 KB
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
class Solution
{
public:
bool checkRedundancy(string &s)
{
stack<char> st;
for (char ch : s)
{
// Push everything except closing bracket
if (ch != ')')
{
st.push(ch);
}
else
{
// Found ')', now check inside brackets
bool hasOperator = false;
while (!st.empty() && st.top() != '(')
{
char top = st.top();
st.pop();
// Check for operator
if (top == '+' || top == '-' || top == '*' || top == '/')
{
hasOperator = true;
}
}
// Remove '('
if (!st.empty())
st.pop();
// If no operator found, brackets are redundant
if (!hasOperator)
return true;
}
}
return false;
}
};