See More

import java.util.*; public class ValidParentheses { public static boolean isValid(String s) { Stack stack = new Stack<>(); for (char c : s.toCharArray()) { if (c == '(' || c == '[' || c == '{') stack.push(c); else { if (stack.isEmpty()) return false; char top = stack.pop(); if ((c == ')' && top != '(') || (c == ']' && top != '[') || (c == '}' && top != '{')) return false; } } return stack.isEmpty(); } public static void main(String[] args) { String input = "{[()]}"; System.out.println(isValid(input) ? "Balanced" : "Not Balanced"); } }