• + 1 comment

    class Solution{

    public static void main(String []argh)
    {
        Scanner sc = new Scanner(System.in);
    
        while (sc.hasNext()) {
         String s = sc.nextLine();
         int n = s.length();
         int counter = 0;
         int counter1 = 0;
         if(s.startsWith("}") || s.startsWith("]") || s.startsWith(")")){
            System.out.println("false");
         }else if(s.endsWith("(") || s.endsWith("{") || s.endsWith("[")){
             System.out.println("false");
         }else {
    
            for (int i = 0; i < n; i++) {
                if (s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{') {
                    counter++;
                }
                if (s.charAt(i) == ')' || s.charAt(i) == ']' || s.charAt(i) == '}') {
                    counter1++;
                }
            }
            if (counter == counter1) {
              System.out.println("true");
            } else {
              System.out.println("false");
            }
        }
     }
    

    } }

    • + 0 comments

      isnt this solution just wrong for this input {(}) ?