waltermilner

Untitled

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