Parenthesis/Brackets Matching using Stack algorithm

前端 未结 30 2725
你的背包
你的背包 2020-11-27 11:39

For example if the parenthesis/brackets is matching in the following:

({})
(()){}()
()

and so on but if the parenthesis/brackets is not mat

30条回答
  •  情歌与酒
    2020-11-27 12:01

    in java you don't want to compare the string or char by == signs. you would use equals method. equalsIgnoreCase or something of the like. if you use == it must point to the same memory location. In the method below I attempted to use ints to get around this. using ints here from the string index since every opening brace has a closing brace. I wanted to use location match instead of a comparison match. But i think with this you have to be intentional in where you place the characters of the string. Lets also consider that Yes = true and No = false for simplicity. This answer assumes that you passed an array of strings to inspect and required an array of if yes (they matched) or No (they didn't)

    import java.util.Stack; 
    
        public static void main(String[] args) {
    
        //String[] arrayOfBraces = new String[]{"{[]}","([{}])","{}{()}","{}","}]{}","{[)]()}"};
        // Example: "()" is balanced
        // Example: "{ ]" is not balanced.
        // Examples: "()[]{}" is balanced.
        // "{([])}" is balanced
        // "{([)]}" is _not_ balanced
    
        String[] arrayOfBraces  = new String[]{"{[]}","([{}])","{}{()}","()[]{}","}]{}","{[)]()}","{[)]()}","{([)]}"};
        String[] answers        = new String[arrayOfBraces.length];     
        String openers          = "([{";
        String closers          = ")]}";
        String stringToInspect  = ""; 
        Stack stack     = new Stack();
    
    
        for (int i = 0; i < arrayOfBraces.length; i++) {
    
            stringToInspect = arrayOfBraces[i];
            for (int j = 0; j < stringToInspect.length(); j++) {            
                if(stack.isEmpty()){
                    if (openers.indexOf(stringToInspect.charAt(j))>=0) {
                        stack.push(""+stringToInspect.charAt(j));   
                    }
                    else{
                        answers[i]= "NO";
                        j=stringToInspect.length();
                    }                   
                }
                else if(openers.indexOf(stringToInspect.charAt(j))>=0){
                    stack.push(""+stringToInspect.charAt(j));   
                }
                else{
                    String comparator = stack.pop();
                    int compLoc = openers.indexOf(comparator);
                    int thisLoc = closers.indexOf(stringToInspect.charAt(j));
    
                    if (compLoc != thisLoc) {
                        answers[i]= "NO";
                        j=stringToInspect.length();                     
                    }
                    else{
                        if(stack.empty() && (j== stringToInspect.length()-1)){
                            answers[i]= "YES";
                        }
                    }
                }
            }
        }
    
        System.out.println(answers.length);         
        for (int j = 0; j < answers.length; j++) {
            System.out.println(answers[j]);
        }       
    }
    

提交回复
热议问题