package com.zerroi.leetcode.Three21; import java.util.Stack; public class IsValidBracket { public static void main(String[] args) { } } class SolutionFirst { public boolean isValid(String s) { Stack stack = new Stack<>(); for (char c : s.toCharArray()) { if (c == '(') stack.push(')'); else if (c == '[') stack.push(']'); else if (c == '{') stack.push('}'); else if (stack.isEmpty() || stack.pop() != c) return false; } return stack.isEmpty(); } }