Професионална програма
Loading...
mitkomik avatar mitkomik 12 Точки
package BalancedBrackets;

import java.util.ArrayDeque;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        ArrayDeque<Character> openDecue = new ArrayDeque<>();

        if (input.length() % 2 != 0) {
            System.out.println("NO");

        } else {

            for (int i = 0; i < input.length(); i++) {
                char temp = input.charAt(i);
                if (temp == '(' || temp == '[' || temp == '{') {
                    openDecue.push(temp);
                } else {
                    switch (temp) {
                        case ')':
                            if (openDecue.peek().equals('('))
                                openDecue.pop();
                            break;
                        case ']':
                            if (openDecue.peek().equals('['))
                                openDecue.pop();
                            break;
                        case '}':
                            if (openDecue.peek().equals('{'))
                                openDecue.pop();
                            break;
                    }
                }
            }
            if (openDecue.isEmpty()) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }
    }
}
0