Java balanced expressions check {[()]}

后端 未结 30 1154
傲寒
傲寒 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:43

    How about this one, it uses both concept of stack plus counter checks:

    import java.util.*;
    class Solution{
    
    public static void main(String []argh)
    {
       Scanner sc = new Scanner(System.in);
       while (sc.hasNext()) {
          String input=sc.next();
          Stack stk = new Stack();
          char[] chr = input.toCharArray();
          int ctrl = 0, ctrr = 0;
          if(input.length()==0){
              System.out.println("true");
          }
          for(int i=0; i

提交回复
热议问题