Sort by

recency

|

1613 Discussions

|

  • + 0 comments

    My C code 😁😎

    char* isBalanced(char* s) {
        if(strlen(s)%2 != 0){
            return "NO";
        }
    
        //parenthese = 0,crochet = 0,bracket = 0;
        int par_g = 0,par_d = 0,cro_g = 0,cro_d = 0,brac_g = 0,brac_d = 0;
    
        for(int i = 0;i<strlen(s);i++){
            if(s[i] == '('){
                par_g++;
            }
            if(s[i] == ')')
            {
                par_d++;
            }
            if(s[i] == '['){
                cro_g++;
            }
            if(s[i] == ']'){
                cro_d++;
            }
            if(s[i] == '{'){
                brac_g++;
            }
            if(s[i] == '}'){
                brac_d++;
            }
            
    
        }
    
        if(((par_d + par_g)%2 != 0) || ((cro_d + cro_g)%2 != 0) || ((brac_g + brac_d)%2 != 0)){
            return "NO";
        }
        if((par_d != par_g) || (cro_d != cro_g) || (brac_d != brac_g)){
            return "NO";
        }
         char stack[10000];
        int top = -1;
    
        for (int i = 0; i < strlen(s); i++) {
            char c = s[i];
            if (c == '(' || c == '{' || c == '[') {
                stack[++top] = c;
            } else {
                if (top == -1)return "NO";
                char topChar = stack[top--];
                if ((c == ')' && topChar != '(') ||
                    (c == '}' && topChar != '{') ||
                    (c == ']' && topChar != '[')) {
                    return "NO";
                }
            }
        }
        return "YES";
    }
    
  • + 1 comment

    My Python submission gives RuntimeError or WrongAnswer in most of the test cases. Anyone knows why?

    def isBalanced(s):
        # Write your code here
        assert len(s) >= 1 and len(s) <= 1e3
        # print(1e3 == 1000)
        
        pairs = {
            "{": "}",
            "(": ")",
            "[": "]"
        }
        brackets = ('{', '}', '(', ')', '[', ']')
        o_brackets = ('{', '(', '[')
        # c_brackets = ('}', ')', ']')
        
        while len(s):
            # print(len(s))
            for c_idx, c in enumerate(s):
                assert c in brackets
                if c in o_brackets:
                    o_c = c
                else:
                    if pairs[o_c] == c:
                        s = s[:c_idx-1]+s[c_idx+1:]
                        break
                    else:
                        return "NO"
        # print(len(s))
        return "YES"
    
  • + 0 comments

    Stack st = new Stack<>(); for(int i=0;i0) return "NO"; else return "YES";

  • + 0 comments

    php solution

    function isBalanced($s) {
        // Write your code here
        $stack = [];
        $left = ["(", "{", "["];
        $right = [")", "}", "]"];
        $len = strlen($s);
        for ($i = 0; $i < $len; $i++) {
            if (0 === $i && in_array($s[$i], $right)) {
                return "NO";
            }
            if (in_array($s[$i], $left)) {
                $stack[] = $s[$i];
                continue;
            }
            switch ($s[$i]) {
                case ")":
                    if ($stack[count($stack) - 1] !== "(") {
                        return "NO";
                    }
                    break;
                case "}":
                    if ($stack[count($stack) - 1] !== "{") {
                        return "NO";
                    }
                    break;
                case "]":
                    if ($stack[count($stack) - 1] !== "[") {
                        return "NO";
                    }
                    break;
            }
            array_pop($stack);
        }
        return empty($stack) ? "YES" : "NO";
    }
    
  • + 0 comments

    private static Boolean cMatch(Character l, Character c){ return ( l == '(' && c == ')') || ( l == '[' && c == ']') || ( l == '{' && c == '}'); } public static String isBalanced(String s) { // Write your code here Stack stack = new Stack<>(); stack.push('#');

        for (int i = 0; i < s.length(); i++){
            Character c = s.charAt(i);
    
            if ( c == '(' || c == '[' || c == '{'){
                stack.push(c);
            }else{
                Character last = stack.peek();
    
                if ( cMatch(last, c )){
                    stack.pop();
                }else{
                    return "NO";
                }     
            }       
        }
    
        if (stack.size() == 1){
            return "YES";
        }
    
        return "NO";
    }