#1614
Easy Algorithms Maximum nesting depth of the parentheses
String Stack
84.8% acceptance
Feb 25, 2026
2813
523
Given a valid parentheses string s, return the nesting depth of s. The nesting depth is the maximum number of nested parentheses.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn max_depth(s: String) -> i32 {
let mut depth = 0i32;
let mut max_depth = 0i32;
for c in s.chars() {
if c == '(' {
depth += 1;
max_depth = max_depth.max(depth);
} else if c == ')' {
depth -= 1;
}
}
max_depth
}
}