Thursday, February 18, 2021

Leetcode Everyday: 1614. Maximum Nesting Depth of the Parentheses. Easy

public class Solution {
    public int MaxDepth(string s) {
        int depth = 0;
        int maxDepth = 0;
        foreach (var c in s) {
            if (c == '(') {
                ++depth;
            } else if (c == ')') {
                --depth;
            }
            if (depth > maxDepth) {
                maxDepth = depth;
            }
        }
        return maxDepth;
    }
}
Source: https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/

No comments:

Post a Comment