Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Problem of the day

Given valid mathematical expressions in the form of a string. You are supposed to return true if the given expression contains a pair of redundant brackets, else return false. The given string only contains ‘(‘, ’)’, ‘+’, ‘-’, ‘*’, ‘/’ and lowercase English letters.

A pair of brackets is said to be redundant when a subexpression is surrounded by needless/ useless brackets.

```
((a+b)) has a pair of redundant brackets. The pair of brackets on the first and last index is needless.
While (a + (b*c)) does not have any pair of redundant brackets.
```

Detailed explanation

```
1 <= T <= 50
3 <= |S| <= 10^4
Time Limit: 1 sec
```

```
2
(a+b)
(a+c*b)+(c))
```

```
No
Yes
```

```
In the first test case, there are no redundant brackets. Hence, the output is “No”.
In the second test case, the brackets around the alphabet ‘c’( index 8 and index 10) are redundant. Hence the output is “Yes”.
```

```
2
(a*b+(c/d))
((a/b))
```

```
No
Yes
```

```
In the first test case, there are no redundant brackets. Hence, the output is “No”.
In the second test case, the brackets around the subexpression “(a+b)” ( index 0 and index 6) are redundant. Hence the output is “Yes”.
```