waltermilner

Untitled

Jun 29th, 2021
1,001
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.90 KB | None | 0 0
  1. public class Test {
  2.  
  3.     static String check(String input) {
  4.         int counter = 0;
  5.         for (int pos = 0; pos < input.length(); pos++) {
  6.             char c = input.charAt(pos);
  7.             if (c == '(') {
  8.                 counter++;
  9.             }
  10.             if (c == ')') {
  11.                 counter--;
  12.             }
  13.             if (counter == -1) {
  14.                 return "False";
  15.             }
  16.         }
  17.         if (counter == 0) {
  18.             return "True";
  19.         } else {
  20.             return "False";
  21.         }
  22.     }
  23.  
  24.     public static void main(String[] args) {
  25.         System.out.println(check("()()")); // true
  26.         System.out.println(check("(()())")); // true
  27.         System.out.println(check(")("));   // false
  28.         System.out.println(check("(()"));  // false
  29.         System.out.println(check("()("));  // false
  30.         System.out.println(check("((((")); // false
  31.     }
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment