Parentheses Validation - Java Algorithm

Java examples for Algorithm:Parser

Description

Parentheses Validation

Demo Code


import java.util.ArrayDeque;
import java.util.Deque;


public class Main {

    public static void main(String[] agrs) {
        System.out.println(validateParentheses("{{}}[]"));
        System.out.println(validateParentheses("{{}}["));
        System.out.println(validateParentheses("{{}}[]]"));
    }//ww w  .  j a  va2s  . co m

    public static boolean validateParentheses(String s) {
        Deque<Character> stack = new ArrayDeque<>();

        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '{' || s.charAt(i) == '[' || s.charAt(i) == '(') {
                stack.add(s.charAt(i));
            } else if (s.charAt(i) == '}' && stack.peekLast() == '{') {
                stack.removeLast();
            } else if (s.charAt(i) == ']' && !stack.isEmpty() && stack.peekLast() == '[' ) {
                stack.removeLast();
            } else if (s.charAt(i) == ')' && stack.peekLast() == '(') {
                stack.removeLast();
            } else {
                return false;
            }
        }
        return stack.isEmpty();
    }
}

Related Tutorials