Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Test {
- static String check(String input) {
- int counter = 0;
- for (int pos = 0; pos < input.length(); pos++) {
- char c = input.charAt(pos);
- if (c == '(') {
- counter++;
- }
- if (c == ')') {
- counter--;
- }
- if (counter == -1) {
- return "False";
- }
- }
- if (counter == 0) {
- return "True";
- } else {
- return "False";
- }
- }
- public static void main(String[] args) {
- System.out.println(check("()()")); // true
- System.out.println(check("(()())")); // true
- System.out.println(check(")(")); // false
- System.out.println(check("(()")); // false
- System.out.println(check("()(")); // false
- System.out.println(check("((((")); // false
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment