
As a programmer, redundancy of any kind is a big NO!
The same goes for braces. The Redundant Braces in an expression only add more computational steps and make the expression look unoptimized.
So, when we are asked to check if the given expression has a redundant bracket or not, it checks the presence of multiple brackets. Also, removing unnecessary braces will not have any effect on the output of the expression.
The redundant braces problem is one of the advanced problems asked in many interviews. This includes other multiple algorithms and basics of programming which makes it one of the top asked questions.
Other questions include the longest common prefix, subsequence, substring, reverse of an array, and more.
In this post, we are going to discuss in detail what are redundant braces and how you can check if the given expression contains unnecessary braces or not.
Problem Statement:
You are given a string of balanced expressions. You need to find if the string contains any redundant braces or not. It is said that a pair of parentheses is redundant if the exact sub-expression consists of multiple and unnecessary braces.
So, you need to print “Yes” if any redundant braces are there. Otherwise, print “No”.
Note that the expression will also have some operators. The given string is valid and contains no white spaces.
Let’s understand the problem statement with an example.
Consider the following strings:
S1: (a+b)
S2: ((a+b))
S3: ((a) +b/ c)
The output of these given strings will be:
No
Yes
Yes
Here’s an explanation:
For S1: (a+b), the string does not contain any redundant braces. Therefore, the output will print “No”.
For S2: ((a+b)), this string can be written as (a+b), and removing the braces will have no effect. Therefore, the output displayed Yes which means that there are redundant braces.
Comments