Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Time Complexity: O(N)
- Space Complexity: O(1)
- N = length of the string
- */
- class Solution {
- public int minAddToMakeValid(String S) {
- if (S == null || S.length() == 0) {
- return 0;
- }
- int result = 0;
- int count = 0;
- for (char s : S.toCharArray()) {
- count += s == '(' ? 1 : -1;
- if (count < 0) {
- result++;
- count = 0;
- }
- }
- return result + count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement