File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# 🎉 Special Day Greetings on the 4th ! 🎉
2+
3+
##### Today, the 4th, holds a special significance for me, and I want to take a moment to share my gratitude with all of you who visit this repository. Your presence here makes this community vibrant and inspiring.
4+
5+
##### Thank you for being part of this journey. Here's to today and all the wonderful possibilities it brings !
6+
7+
8+
## Today's 04-04-24 [Problem Link](https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/description/?envType=daily-question&envId=2024-04-04)
9+
## 1614. Maximum Nesting Depth of the Parentheses
10+
11+
# Intuition
12+
<!-- Describe your first thoughts on how to solve this problem. -->
13+
- I aim to find the maximum depth of nested parentheses in a given string.
14+
# Approach
15+
<!-- Describe your approach to solving the problem. -->
16+
- I initialized two variables, `jawab` and `khula`, to keep track of the maximum depth and current depth, respectively.
17+
- Iterated through each character `c` in the string `s`.
18+
- If `c` is an opening parenthesis '(', incremented `khula` and update `jawab` to the maximum of its current value and `khula`.
19+
- If `c` is a closing parenthesis ')', decremented `khula`.
20+
- Finally, returned `jawab` as the maximum depth of parentheses.
21+
22+
---
23+
Have a look at the code , still have any confusion then please let me know in the comments
24+
Keep Solving.:)
25+
# Complexity
26+
- Time complexity : $O(n)$
27+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
28+
$n$ : length of the given string
29+
- Space complexity : $O(1)$
30+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
31+
32+
# Code
33+
```
34+
class Solution {
35+
public int maxDepth(String s) {
36+
37+
// Initialize variables
38+
int jawab = 0; // Store the maximum depth found so far
39+
int khula = 0; // Keep track of the current depth
40+
41+
// Iterate through each character in the string
42+
for (final char c : s.toCharArray()){
43+
// If the character is an opening parenthesis
44+
if (c == '('){
45+
khula++; // Incrementing the current depth
46+
jawab = Math.max(jawab, khula); // Updating maximum depth if necessary
47+
}
48+
// If the character is a closing parenthesis
49+
else if (c == ')'){
50+
khula--; // Decrementing the current depth
51+
}
52+
}
53+
// Returning the maximum depth found
54+
return jawab;
55+
}
56+
}
57+
```

0 commit comments

Comments
 (0)