1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { public int maxDepth(String s) { int maxDepth = 0;
int leftBrackets = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == '(') { leftBrackets++; maxDepth = Math.max(maxDepth, leftBrackets); } else if (c == ')') { leftBrackets--; } }
return maxDepth; } }
|
References
1614. Maximum Nesting Depth of the Parentheses