602. Count Pairs of Matching Parentheses

EasyStackStackString

Given a string s of '(' and ')', return the number of matched parenthesis pairs — each ')' that has an available unmatched '(' before it forms one pair. Unmatched characters are ignored. The input is JSON {s}.

Input: JSON {s}.

Output: Integer — the number of matched pairs.

Examples

Example 1
Input: {"s":"(())"}
Output: 2
Explanation: Two nested pairs.
Example 2
Input: {"s":"(()"}
Output: 1
Explanation: One pair matches; an open is unmatched.
Example 3
Input: {"s":")("}
Output: 0
Explanation: No valid pair.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →