You are given a string s made only of the characters ( and ).
A piece of s is valid when every ( in it has its own ) that comes later, and every ) has its own ( that comes earlier. The brackets must also close in the right order, like (()) or ()().
Look at all pieces of s made of characters that sit side by side. Return the length of the longest valid piece. If there is no valid piece, return 0.
Examples
Example 1:
Input: s = "(()))"
Output: 4
Explanation: The first four characters, "(())", are valid. The last ")" has no partner.
Example 2:
Input: s = "()(())"
Output: 6
Explanation: The whole string is valid, so the answer is its full length.
Example 3:
Input: s = ""
Output: 0
Explanation: An empty string has no valid piece.