Sort by

recency

|

1635 Discussions

|

  • + 0 comments

    JAVA 8

    public static String isBalanced(String s) { String response = "YES"; String[] values = s.split(""); String listExpected = ""; String passValues = "({[";

    for(int i = 0; i < values.length; i ++){
        String value = values[i];
    
        if(!passValues.contains(value)){
            String[] list = listExpected.split("");
            if(list[0].equals(value)){
                listExpected = listExpected.replaceFirst("\\"+value, "");
            } else {
                response = "NO";
                break;
            }
        }
    
        if(value.equals("(")){
            listExpected = ")" + listExpected;
        }
    
        if(value.equals("{")){
            listExpected = "}" + listExpected;
        }
    
        if(value.equals("[")){
            listExpected = "]" + listExpected;
        }
    }
    
    return response;
    }
    
  • + 0 comments
    def isBalanced(s):
        _map={')': '(', ']':'[', '}':'{'}
        q=[] 
        for character in s:
            if character in '([{': 
                q.append(character)
            elif character in ')]}': 
                if not q: 
                    return 'NO'
                last=q.pop()
                if last!=_map[character]: 
                    return 'NO'
        return 'YES' if not q else 'NO'
    
  • + 0 comments

    use a lifo queue and scan left to right

  • + 0 comments

    What's the issue?

    include

    using namespace std; void printstack(stack s){ while(!s.empty()){ cout< st; bool t=true; for (auto ch:s){ if ((ch=='(')||(ch=='[') || (ch=='{')){ st.push(ch); //printstack(st); }else{ if ((ch==')') && (st.top()=='(')){ st.pop(); }else if((ch==']') &&(st.top()=='[')){ st.pop(); }else if((ch=='}') && (st.top()=='{')){ st.pop(); }else{ return "NO"; } } }

    if (st.empty()){
        return "YES";
    }else{
        return "NO";
    }
    

    } int main(){ int t; cin>>t; while(t--){ string s; cin>>s; cout<

  • + 0 comments

    I try a recursive solution just for fun (won't pass the test cases)

        # rest of the code ...
        # the methods are naive methods and self explained.
        # `stack` variable could be considered as a global variable.
        def isBalanced(self, s: str, stack: deque) -> bool:
            c: str = s[0]
            
            if self.isOpenBracket(c):
                stack.append(c)
            else:
                stack_top: str = stack.pop()
                if not self.areBothBracketsBalanced(stack_top, c):
                    print('\tnot-match: ', stack_top, c)
    
                    return False
                
                if len(s) == 1 and len(stack) == 0:
                    print('\tfinal-case OK')
                    
                    return True
            
            # New solution.
            new_sol: str = s[1:]
            
            if len(new_sol) > 0:
                return self.isBalanced(new_sol, stack)