Stacks: Balanced Brackets

Sort by

recency

|

409 Discussions

|

  • + 0 comments

    Python O(n)

    def isBalanced(expression):
        stack = []
        for ind in range(len(expression)):
            elem = ''
            if expression[ind] in '([{':
                stack.append(expression[ind])
            elif len(stack) > 0:
                elem = stack.pop()
            else:
                return 'NO'
            
            if (expression[ind] in ')' and elem != '(') \
                or (expression[ind] in '}' and elem != '{') \
                or (expression[ind] in ']' and elem != '['):
                return 'NO'
        return 'YES' if len(stack) == 0 else 'NO'
    
  • + 0 comments

    Clean Ruby solution:

    BRACKETS = {
      '(' => ')',
      '{' => '}',
      '[' => ']'
    }
    
    def is_balanced(expression)
      stack = []
      expression.each_char do |c|
        if BRACKETS.key? c
          stack << c
        elsif BRACKETS[stack.pop] != c
          return false
        end
      end
      stack.empty?
    end
    
    gets.to_i.times do
      puts is_balanced(gets.chomp) ? 'YES' : 'NO'
    end
    
  • + 0 comments

    JavaScript Solution: ========= PSEUDOCODE ========== 1. Take one empty stack and a variable name valid with true initial value 2. Travell to each bracket of the input 3. if we are having left bracket push it into the stack 4. if we are having right bracket , then pop a bracket from the stack. . check if the poped bracked and the right bracket are compliment to eachother or not OR check stack is empty , and update the variable valid accordingly. . complete it for all three type of right bracket 5. if valid == true and stack is empty return 'YES' otherwise return 'NO'.

    Here is working code :

    function isBalanced(S) {
        let valid = true
        let stack = []
        for(let c of S){
            if(c=='(' || c=='[' || c=='{') stack.push(c)  
            else if(c == ')'){
                 valid = (stack.length === 0 || stack.pop() != '(') ? false : valid
            }
            else if(c == ']'){
                 valid = (stack.length === 0 || stack.pop() != '[' ) ? false : valid
            }
            else if(c == '}'){
                 valid = (stack.length === 0 || stack.pop() != '{' ) ? false : valid
            }
        }
        return  (valid && stack.length ===0 ) ? 'YES' : 'NO'
    }
    
  • + 0 comments

    **Getting seg fault please someone help **

    #include<iostream>
    #include<stack>
    using namespace std;
    bool isBalance(string s,int i=0)
    {
        stack<char>stackk;
        for(int i=0;i<s.length();i++)
        {
                if(s[i]=='[' || s[i]=='{' || s[i]=='(')
                {
                    stackk.push(s[i]);
                }
                else if(s[i]==']' && stackk.top()!='[')
                {
                    return false;
                }
                else if(s[i]==')' && stackk.top()!='(')
                {
                    return false;
                }
                else if(s[i]=='}' && stackk.top()!='{')
                {
                    return false;
                }
                else
                {
                    stackk.pop();
                }
        }
        return true;
    }
    int main()
    {
      int n;
      cin>>n;
      while(n--)
      {
        string s;
        cin>>s;
        if(isBalance(s))
        {
          cout<<"YES"<<endl;
        }
        else
        {
          cout<<"NO"<<endl;
        }
      }
        return 0;
    }
    
  • + 0 comments

    ** Getting seg fault please someone help **

    #include<iostream>
    #include<stack>
    using namespace std;
    bool isBalance(string s,int i=0)
    {
        stack<char>stackk;
        for(int i=0;i<s.length();i++)
        {
                if(s[i]=='[' || s[i]=='{' || s[i]=='(')
                {
                    stackk.push(s[i]);
                }
                else if(s[i]==']' && stackk.top()!='[')
                {
                    return false;
                }
                else if(s[i]==')' && stackk.top()!='(')
                {
                    return false;
                }
                else if(s[i]=='}' && stackk.top()!='{')
                {
                    return false;
                }
                else
                {
                    stackk.pop();
                }
        }
        return true;
    }
    int main()
    {
      int n;
      cin>>n;
      while(n--)
      {
        string s;
        cin>>s;
        if(isBalance(s))
        {
          cout<<"YES"<<endl;
        }
        else
        {
          cout<<"NO"<<endl;
        }
      }
        return 0;
    }