Balanced Brackets

Sort by

recency

|

63 Discussions

|

  • + 0 comments

    Java Solution:

        public static String isBalanced(String s) {
            char[] arr = s.toCharArray();
            Stack<Character> stack = new Stack<>();
            for(char c : arr){
                if(c == '{' || c == '(' || c == '['){
                    stack.push(c);
                }
                
                if(c == '}' || c == ')' || c == ']'){
                    if(stack.isEmpty()) return "NO";
                    char pop = stack.pop();
                    if(c == '}' && pop != '{'){
                        return "NO";
                    }
                    if(c == ')' && pop != '('){
                        return "NO";
                    }
                    if(c == ']' && pop != '['){
                        return "NO";
                    }
                }
            }
            
            if(stack.isEmpty()) return "YES";
            
            return "NO";
        }
    
  • + 0 comments
    def isBalanced(s):
        stack = []
        open_brackets = ['(', '[', '{']
        close_brackets = [')', ']', '}']
        
        result = 'YES'
        
        if len(s) > 1:
            for char in s:
                if char in open_brackets:
                    stack.append(char)
                elif char in close_brackets:
                    if (
                        (char == ')' and stack and stack[-1] == '(') or
                        (char == ']' and stack and stack[-1] == '[') or
                        (char == '}' and stack and stack[-1] == '{')
                    ):
                        stack.pop()
                    else:
                        result = 'NO'
                        break
    
        else:
            result = 'NO'
    
        if stack or result == 'NO':
            return 'NO'
    
        return 'YES'
    
  • + 0 comments
    from functools import reduce
    
    def isBalanced(s):
        return 'NO' if reduce(lambda x, y: x.replace('()', '').replace('[]', '').replace('{}', ''), s[:len(s)//2+1], s) else 'YES'
    
  • + 0 comments

    python with a map

    brackets = {
        "}": "{",
        ")": "(",
        "]": "["
    }
    
    def isBalanced(s):
        # Write your code here
        stack = []
        for x in s:
            if x in brackets.values():
                stack.append(x)
            elif stack and brackets[x] == stack[-1]:
                stack.pop()
            else:
                return "NO"
        if not stack:
            return "YES"
        return "NO"
                
    
  • + 0 comments

    Quick solution in Python 3:

    def isBalanced(s):
        history = []
        
        for bracket in s:
            if bracket == '(':
                history.append(')')
            elif bracket == '{':
                history.append('}')
            elif bracket == '[':
                history.append(']')
            else:
                if not history or bracket != history.pop():
                    return 'NO'
                    
        return 'YES' if not history else 'NO'