#921

Minimum Add to Make Parentheses Valid

pupil · 500 · lc medium +27 · verified · 74.4% accepted · 4,932 likes · top 85%

Description

A parentheses string is valid if it is empty, the concatenation of two valid strings, or a valid string enclosed in a matching pair. Given a string s of '(' and ')' characters, return the fewest characters that must be inserted to make s valid.

Example 1:

Input: s = "())"
Output: 1

Example 2:

Input: s = "((("
Output: 3

Code

1
2
3