Monday, November 23, 2020

Balanced Parantheses! | InterviewBit | Amazon | Google

 

Problem : 


Given a string A consisting only of '(' and ')'.


You need to find whether parantheses in A is balanced or not ,if it is balanced then return 1 else return 0.



Problem Constraints



1 <= |A| <= 105



Input Format

First argument is an string A.



Output Format

Return 1 if parantheses in string are balanced else return 0.


Solution :

int Solution::solve(string A) {

    int l = A.length();

    stack<char>s;

    int f = 0;

    for(int i =0;i<l;i++){

        char c  = A[i];

        if(c == '('){

            s.push(c);

        }else{

            if(s.empty()){

                f = 1;

                break;

            }

            s.pop();

            }

        

    }

    if(f == 1){

        return 0;

    }

    if(!s.empty()){

        return 0;

    }

    return 1;

}





No comments:

Post a Comment