Valid parentheses · Compiler
Valid parentheses
Scan the parentheses from left to right. Each closing parenthesis uses one earlier unmatched opening parenthesis. Report the first closing parenthesis for which none is available. If every closing parenthesis has a partner, distinguish leftover openings from a fully balanced string.
- Input
- One line of 1 to 200 characters, each either ( or ). Positions start at 1.
- Output
- Print the position of the first unpaired closing parenthesis if one occurs. Otherwise print OPEN if any opening parenthesis remains unmatched, or OK if the entire string is balanced.
Compiler Explorer runs your code when you run or check it.
Loading your exercise draft…