Given a string of parentheses, find the maximum nesting depth. This simulates finding the height of a tree where '(' means go deeper.
Example Input & Output
Example 1
Input
"(()())"
Output
2
Example 2
Input
"()"
Output
1
Example 3
Input
""
Output
0
Example 4
Input
"((()))"
Output
3
Example 5
Input
"(())()"
Output
2
Algorithm Flow

Recommendation Algorithm Flow for Tree Maximum Depth
Solution Approach
Best Answers
java
class Solution {
public int solution(String s) {
int m=0,c=0;
for(int i=0;i<s.length();i++){if(s.charAt(i)=='('){c++;m=Math.max(m,c);}else c--;}
return m;
}
}Related Tree Challenges
Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
