Skip to content

Commit b5faba1

Browse files
authored
Create 1614. Maximum Nesting Depth of the Parentheses (#447)
2 parents 87970c9 + aca43b6 commit b5faba1

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
// Method to find the maximum depth of nested parentheses in a given string.
4+
int maxDepth(std::string s) {
5+
// Initializing variables to keep track of depth and count of open parentheses.
6+
int depth = 0; // Represents the maximum depth of nested parentheses encountered.
7+
int count = 0; // Keeps track of the number of open parentheses encountered.
8+
9+
// Iterating through each character in the string.
10+
for (char ch : s) {
11+
// If the character is an open parenthesis '(',
12+
// increment the count of open parentheses encountered.
13+
if (ch == '(') {
14+
count++;
15+
// Update the depth with the maximum of the current depth and the count of open parentheses.
16+
depth = std::max(depth, count);
17+
}
18+
19+
// If the character is a closing parenthesis ')',
20+
// decrement the count of open parentheses encountered.
21+
if (ch == ')') {
22+
count--;
23+
}
24+
}
25+
26+
// Return the maximum depth found.
27+
return depth;
28+
}
29+
};

0 commit comments

Comments
 (0)