File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments