1614. Maximum Nesting Depth of the Parentheses
/**
* Problem: LeetCode 1614 - Maximum Nesting Depth of the Parentheses
* Difficulty: Easy
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int maxDepth(String s) {
int depth = 0;
int maxDepth = 0;
for (char c : s.toCharArray()) {
if (c == '(') {
depth++;
maxDepth = Math.max(maxDepth, depth);
} else if (c == ')') {
depth--;
}
}
return maxDepth;
}
}
"""
Problem: LeetCode 1614 - Maximum Nesting Depth of the Parentheses
Difficulty: Easy
Language: Python
Author: CodeByTushu
"""
class Solution:
def maxDepth(self, s: str) -> int:
depth = 0
max_depth = 0
for c in s:
if c == '(':
depth += 1
max_depth = max(max_depth, depth)
elif c == ')':
depth -= 1
return max_depth
/**
* Problem: LeetCode 1614 - Maximum Nesting Depth of the Parentheses
* Difficulty: Easy
* Language: C++
* Author: CodeByTushu
*/
#include <string>
#include <algorithm>
using namespace std;
/**
* Problem: LeetCode 1614 - Maximum Nesting Depth of the Parentheses
* Difficulty: Easy
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
int maxDepth(string s) {
int depth = 0;
int maxDepth = 0;
for (char c : s) {
if (c == '(') {
depth++;
maxDepth = max(maxDepth, depth);
} else if (c == ')') {
depth--;
}
}
return maxDepth;
}
};
/**
* Problem: LeetCode 1614 - Maximum Nesting Depth of the Parentheses
* Difficulty: Easy
* Language: JavaScript
* Author: CodeByTushu
*/
var maxDepth = function(s) {
let depth = 0;
let maxDepth = 0;
for (const c of s) {
if (c === '(') {
depth++;
maxDepth = Math.max(maxDepth, depth);
} else if (c === ')') {
depth--;
}
}
return maxDepth;
};