We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Balanced Brackets
  2. Discussions

Balanced Brackets

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 152 Discussions, By:

recency

Please Login in order to post a comment

  • Jabboscurio
    3 days ago+ 0 comments

    Python 3 solution

    def isBalanced(s):
        op = []
        for i in range(len(s)):
            if s[i] in '({[':
                op.append(s[i])
            elif s[i] in ')}]':
                try:
                    if op[-1]+s[i] == '()' or op[-1]+s[i] == '{}' or op[-1]+s[i] == '[]':
                        op.pop(-1)
                    else:
                        return 'NO'
                except:
                    return 'NO'
        if len(op) != 0:
            return 'NO'
        else:
            return 'YES'
    
    0|
    Permalink
  • Sushmitha93
    5 days ago+ 0 comments

    JavaScript Solution

    function isBalanced(s) {
        let stack=[], top=-1, map=new Map([[')','('],['}','{'],[']','[']])
        for(let i=0;i<s.length;i++){
            if(/[\)\}\]]/.test(s[i])){
                if(top>=0 && map.get(s[i])==stack[top] ) top--
                else return "NO"
            }
            else stack[++top]=s[i]
        }
        return top>=0?"NO":"YES"
    }
    
    0|
    Permalink
  • imranansari9836
    2 weeks ago+ 0 comments

    Java Eassy: import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;

    public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        tests:
        for(int a0 = 0; a0 < t; a0++){
            String s = in.next();
            Stack<Character> stack = new Stack<>();
    
            for(char c : s.toCharArray())
            {
                if(c == '(')
                    stack.push(')');
                else if(c == '{')
                    stack.push('}');
                else if(c == '[')
                    stack.push(']');
    
                else{
                    if( stack.isEmpty() || c != stack.peek()){
                        System.out.println("NO");
                        continue tests;    
                    }
                    else{
                        stack.pop();
                    }
                }
            }
            if(stack.isEmpty())
                System.out.println("YES");
            else
                System.out.println("NO");
        }
    }
    

    }

    0|
    Permalink
  • sdsfji_game
    2 weeks ago+ 0 comments

    C#

        public static string isBalanced(string s)
        {
            Stack<char> left = new Stack<char>();
            int lastLeftIndex = 0;
            for(int i = 0; i < s.Length; i++)
            {
                if(s[i] == '(' || s[i] == '{' || s[i] == '[')
                {
                    left.Push(s[i]);
                }
                else if(s[i] == ')' && (left.Count == 0 || left.Pop() != '('))
                {
                    return "NO";
                }
                else if(s[i] == '}' && (left.Count == 0 ||left.Pop() != '{'))
                {
                    return "NO";
                }
                else if(s[i] == ']' && (left.Count == 0 ||left.Pop() != '['))
                {
                    return "NO";   
                }
            }
            if(left.Count > 0)
            {
                return "NO";
            }
            return "YES";
        }
    
    0|
    Permalink
  • stbo1
    3 weeks ago+ 0 comments

    Python Recursive solution

    def checkString(s): 
        # function to check if there's a match 
        original = s
        s = s.replace("{}","")
        s = s.replace("[]","")
        s = s.replace("()","")
        if s==original:
            return None 
        else: 
            return s
        
    def isBalanced(s):
        # Write your code here
        while (len(s)!=0):
            s = checkString(s) 
            if s is None: 
                return "NO" 
            
        return "YES"
        
    
    1|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy