Java balanced expressions check {[()]}

后端 未结 30 1139
傲寒
傲寒 2020-12-04 17:17

I am trying to create a program that takes a string as an argument into its constructor. I need a method that checks whether the string is a balanced parenthesized expressio

30条回答
  •  执念已碎
    2020-12-04 17:33

    Similar to one of the code above in JAVA but It needs one more else statement added in order to avoid stack comparison with characters other than braces :

    else if(bracketPair.containsValue(strExpression.charAt(i)))

    public boolean isBalanced(String strExpression){
     Map bracketPair = new HashMap();
      bracketPair.put('(', ')');
      bracketPair.put('[', ']');
      bracketPair.put('{', '}');
      Stack stk = new Stack();
            for(int i =0;i

提交回复
热议问题