Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args)
- {
- System.out.println("Enter string: ");
- Scanner sc = new Scanner(System.in);
- String str = sc.nextLine();
- Stack<Character> stack = new Stack<>();
- for (char c : str.toCharArray())
- {
- switch (c)
- {
- case '(' :
- case '[':
- stack.push(c);
- break;
- case ')':
- if (stack.isEmpty() || stack.pop() != '(')
- {
- System.out.println("Incorrect");
- return;
- }
- break;
- case ']':
- if (stack.isEmpty() || stack.pop() != '[')
- {
- System.out.println("Incorrect");
- return;
- }
- break;
- }
- }
- System.out.println(stack.empty() ? "Correct" : "Incorrect");
- }
Add Comment
Please, Sign In to add comment