> For the complete documentation index, see [llms.txt](https://ucan.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ucan.gitbook.io/notes/algorithm/659.-split-array-into-consecutive-subsequences/921.-minimum-add-to-make-parentheses-valid.md).

# 921. Minimum Add to Make Parentheses Valid

```java
class Solution {
    public int minAddToMakeValid(String s) {
        int result = 0;
        //when see a '(', need++ meaning will need a ')';
        int need =0;
        for (int i=0;i<s.length(); i++){
            if(s.charAt(i)=='('){
                need++;
            }else{ //if see')'
                if(need==0){
                    result++;
                }else{
                    need--;
                }
            }
        }
        return result+need;
    }
}
```
