• + 0 comments

    Please this my implementation if anyone note vulnerability please told me

    import java.util.*;
    
    public class Solution {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            while (sc.hasNext()) {
                String input = sc.next();
                System.out.println(validate(input));
            }
        }
    
        public static boolean validate(String input) {
            Stack<String> stack = new Stack<String>();
            List<String> characterList = new ArrayList<>(Arrays.asList(input.split("")));
            StringBuilder compareStr = new StringBuilder();
            characterList.forEach(c -> {
                switch (c) {
                    case "(":
                        stack.push(c);
                        compareStr.append(c);
                        break;
                    case "{":
                        stack.push(c);
                        compareStr.append(c);
                        break;
                    case "[":
                        stack.push(c);
                        compareStr.append(c);
                        break;
                    case ")":
                        if (!stack.empty() && stack.peek().equals("(")) {
                            compareStr.append(c);
                            stack.pop();
                        }
                        break;
                    case "}":
                        if (!stack.empty() && stack.peek().equals("{")) {
                            compareStr.append(c);
                            stack.pop();
                        }
                        break;
                    case "]":
                        if (!stack.empty() && stack.peek().equals("[")) {
                            compareStr.append(c);
                            stack.pop();
                        }
                        break;
                }
            });
            if (!compareStr.toString().equals(input)) {return false;}
            if (stack.empty()) {
                return true;
            }
            return false;
        }
    }