Ask a Question

 

Start Coding Today

   

GeeksQuiz

GeeksforGeeksIDE

Data Structures

Algorithms

Interview Experiences

C Programming

C++ Programming

Java Programming

GATE CS

Books

Program to check redundant braces.

Write a program to validate if the input string has redundant braces?
Return 0/1 
 0 -> NO 1 -> YES 

Input will be always a valid expression

and operators allowed are only + , * , - , /

Example:

((a+b)) has redundant braces so answer will be 1
(a + (a + b)) doesn't have have any redundant braces so answer will be 0

 

asked Oct 6, 2015 by Swanky

4 Answers

+1 vote

If we somehow pick out sub-expressions surrounded by ( and ), then if we are left with () as a part of the string, we know we have redundant braces.

Lets take an example:

(a+(a+b))

We keep pushing elements onto the stack till we encounter ')'. 
When we do encounter ')', we start popping elements till we 
find a matching '('. If the number of elements popped do not 
correspond to '()', we are fine and we can move forward. 
Otherwise, voila! Matching braces have been found. 

Note Cases Like :(a*(a)) and (a) etc.
 

#include<bits/stdc++.h>
using namespace std;

int solve(string str) {
	
	int N = str.size();
    stack<char> Stk;

	for(int i = 0; i < N; ++i) {
        
		if(str[i] == ')') {
        
		    int cnt = 0;
        
		    while(Stk.top() != '(') {
                    Stk.pop();
                    cnt++;
            }
        
		    Stk.pop();
            if(cnt < 2)
                    return 1;
        } 
		
		else {
            Stk.push(str[i]);
        }
	}
        
    bool is = true;
    
	while(Stk.size()) {
        
		if(Stk.top() == '(' || Stk.top() == ')') {
                is = false;
                break;
        }
        Stk.pop();
    }

    if(!is) return 1;
    return 0;
}

int main() {
	
	cout << solve("(a + (a + b))") << endl;
	
	cout << solve("a*(a)") << endl;
	return 0;
}

 

answered Oct 6, 2015 by Gaurav
bool is = true;
    
	while(Stk.size()) {
        
		if(Stk.top() == '(' || Stk.top() == ')') {
                is = false;
                break;
        }
        Stk.pop();
    }

    if(!is) return 1;
    return 0;

 

This part is not required i think.

+1 vote

int solve(string A) {

stack<char> s;
    for(int i=0; i<A.length();++i)
    {
        if(A[i]==')')
        {
            int cnt = 0;
            while(s.top()!='(')
            {
                s.pop();
                cnt++;
            }
            s.pop();
            if(cnt<2)
                return 1;
        }
        else
         s.push(A[i]);
    }
    return 0;

}

answered Aug 14, 2016 by anonymous
reshown Mar 30 by Shubham Baranwal 1
0 votes

Solution in JAVA:

import java.util.*;
public class Braces {
    public static void main(String...okok)
    {
        Scanner sc= new Scanner(System.in);
        String str=sc.next();
        System.out.println(braces(str));
    }
    public static int braces(String str) {
        
        int N = str.length();
        Stack<Character> Stk= new Stack<Character>();
      
     
        for(int i = 0; i < N; ++i) {
             
            if(str.charAt(i) == ')') {
             
                int cnt = 0;
             
                while(Stk.peek() != '(') {
                        Stk.pop();
                        cnt++;
                }
             
                Stk.pop();
                if(cnt < 2)
                        return 1;
            } 
             
            else {
                Stk.push(str.charAt(i));
            }
          
        }
     
        boolean is = true;
         
        while(Stk.size()>0) {
             
            if(Stk.peek() == '(' || Stk.peek() == ')') {
                    is = false;
                    break;
            }
            Stk.pop();
        }
     
        if(!is)
        {
                return 1;
        }
        return 0;
    }

}

answered Dec 4, 2015 by Prabhunath Yadav
0 votes
public class Solution {
	public int braces(String a) {
	    
	    int aLen = a.length();
	    
	    //custom stack
	    char[] stk = new char[aLen];
	    int top = -1;
	    char topElement;
	    
	    char ch ;
	    for(int i = 0 ; i < aLen ; i++){
	         ch = a.charAt(i);
	        if(ch==')'){
	            //check top element
	            topElement = stk[top--];
	            if(topElement=='('){
	                return 1;
	            }else{
	                //remove all operators '+' , '*', '-' and '/' 
	                // untill top most '(' is encountered and also 
	                // remove that top most '(' character from stack
	                while((topElement = stk[top--])!='(');
	            }
	        }else if(ch=='('||ch=='+'
	        ||ch=='*'||ch=='-'||ch=='/'){
	            //add characters to stack if it is '(', '+' , 
	            //'*', '-' or '/' 
	           stk[++top]=ch;
	        }
	    }
	    return 0;
	    
	}
}

 

answered May 11 by Rajkishore Hembram

Please log in or register to answer this question.

...