Skip to content

Create 1614. Maximum Nesting Depth of the Parentheses #447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions 1614. Maximum Nesting Depth of the Parentheses
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
// Method to find the maximum depth of nested parentheses in a given string.
int maxDepth(std::string s) {
// Initializing variables to keep track of depth and count of open parentheses.
int depth = 0; // Represents the maximum depth of nested parentheses encountered.
int count = 0; // Keeps track of the number of open parentheses encountered.

// Iterating through each character in the string.
for (char ch : s) {
// If the character is an open parenthesis '(',
// increment the count of open parentheses encountered.
if (ch == '(') {
count++;
// Update the depth with the maximum of the current depth and the count of open parentheses.
depth = std::max(depth, count);
}

// If the character is a closing parenthesis ')',
// decrement the count of open parentheses encountered.
if (ch == ')') {
count--;
}
}

// Return the maximum depth found.
return depth;
}
};
Loading