Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Braces {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String str = sc.nextLine();
- char[] braces = str.toCharArray();
- int count1 = 0; // counter for ()
- int count2 = 0; // counter for {}
- int count3 = 0; // counter for []
- boolean sup = true;
- int i = 0;
- while(true) {
- if(braces[i] == '(') {
- count1++;
- }
- if(braces[i] == ')') {
- count1--;
- }
- if(braces[i] == '{') {
- count2++;
- }
- if(braces[i] == '}') {
- count2--;
- }
- if(braces[i] == '[') {
- count3++;
- }
- if(braces[i] == ']') {
- count3--;
- }
- if (count1 < 0 || count2 < 0 || count3 < 0) {
- sup = false;
- }
- i++;
- if(i >= braces.length) break;
- }
- if(sup) {
- System.out.println("Ok");
- }
- else {
- System.out.println("Not ok");
- }
- }
- }
Add Comment
Please, Sign In to add comment