import java.util.ArrayDeque; import java.util.Scanner; public class LongestValidParentheses_03 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); System.out.println(longestValidParentheses(text)); } public static int longestValidParentheses(String s) { ArrayDeque stack = new ArrayDeque<>(); int result = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == ')') { if (!stack.isEmpty() && stack.peek()[0] == 0) { stack.pop(); result = Math.max(result, i - (stack.isEmpty() ? -1 : stack.peek()[1])); } else { stack.push(new int[]{1, i}); } } else { stack.push(new int[]{0, i}); } } return result; } }