Advertisement
K_S_

Untitled

Nov 20th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1. // Runtime: 0 ms, faster than 100.00% of Java online submissions for Minimum Add to Make Parentheses Valid.
  2. // Memory Usage: 34.3 MB, less than 100.00% of Java online submissions for Minimum Add to Make Parentheses Valid.
  3.  
  4. class Solution {
  5.     public int minAddToMakeValid(String S) {
  6.         char[] chars = S.toCharArray();
  7.  
  8.         int left = 0;
  9.         int right = 0;
  10.         for (char ch : chars) {
  11.             if(ch == '(') {
  12.                 left++;
  13.             } else {
  14.                 if(left > 0) {
  15.                     left--;
  16.                 } else {
  17.                     right++;
  18.                 }
  19.             }
  20.         }
  21.  
  22.         return left + right;
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement