|
| 1 | + |
| 2 | +#🎉 Special Day Greetings on the 4th! 🎉 |
| 3 | + |
| 4 | +## Hello everyone, |
| 5 | +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. |
| 6 | + |
| 7 | +Thank you for being part of this journey. Here's to today and all the wonderful possibilities it brings! |
| 8 | + |
1 | 9 | # Leetcode Daily Challenge Solutions
|
2 | 10 |
|
3 | 11 | This is my attempt to make the coding experience easier for you guys so that you can easily learn what to do in today's leetcode challenge.
|
4 | 12 |
|
5 | 13 | ## Always here to assist you guys.
|
6 | 14 |
|
7 |
| -## Today's 03-04-24 [Problem Link](https://leetcode.com/problems/word-search/description/?envType=daily-question&envId=2024-04-03) |
8 |
| -## 79. Word Search |
| 15 | +## 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) |
| 16 | +## 1614. Maximum Nesting Depth of the Parentheses |
9 | 17 |
|
10 | 18 | # Intuition
|
11 | 19 | <!-- Describe your first thoughts on how to solve this problem. -->
|
12 |
| -I should utilize Depth-First Search (DFS) to systematically explore the board, aiming to find the given word by traversing adjacent cells. |
13 |
| - |
| 20 | +- I aim to find the maximum depth of nested parentheses in a given string. |
14 | 21 | # Approach
|
15 | 22 | <!-- Describe your approach to solving the problem. -->
|
| 23 | +- I initialized two variables, `jawab` and `khula`, to keep track of the maximum depth and current depth, respectively. |
| 24 | +- Iterated through each character `c` in the string `s`. |
| 25 | +- If `c` is an opening parenthesis '(', incremented `khula` and update `jawab` to the maximum of its current value and `khula`. |
| 26 | +- If `c` is a closing parenthesis ')', decremented `khula`. |
| 27 | +- Finally, returned `jawab` as the maximum depth of parentheses. |
16 | 28 |
|
17 |
| -**Main Method (exist) :** |
18 |
| -- Iterated through each cell on the board. |
19 |
| -- Initiated DFS exploration from each cell to search for the word. |
20 |
| -- Returned true if the word is found, otherwise false. |
21 |
| - |
22 |
| -**DFS Method (explore) :** |
23 |
| -- Checked if the current cell is within bounds. |
24 |
| -- Verified if the character at the current cell matches the word's character. |
25 |
| -- Recursively explored adjacent cells to find the next character of the word. |
26 |
| -- Marked visited cells to avoid revisiting and backtrack when necessary. |
27 |
| -- If the entire word is found, returned true; otherwise, returned false. |
28 |
| - |
29 | 29 | ---
|
30 | 30 | Have a look at the code , still have any confusion then please let me know in the comments
|
31 | 31 | Keep Solving.:)
|
32 | 32 | # Complexity
|
33 |
| -- Time complexity : $O(m \times n \times 4^l)$ |
| 33 | +- Time complexity : $O(n)$ |
34 | 34 | <!-- Add your time complexity here, e.g. $$O(n)$$ -->
|
35 |
| -$m$ : number of rows |
36 |
| - |
37 |
| -$n$ : number of columns |
38 |
| - |
39 |
| -$l$ : length of the word |
| 35 | +$n$ : length of the given string |
40 | 36 | - Space complexity : $O(1)$
|
41 | 37 | <!-- Add your space complexity here, e.g. $$O(n)$$ -->
|
42 | 38 |
|
43 | 39 | # Code
|
44 | 40 | ```
|
45 | 41 | class Solution {
|
46 |
| -// Main method to check if the word exists on the board |
47 |
| -public boolean exist(char[][] board, String word) { |
48 |
| -for (int row = 0; row < board.length; ++row){ |
49 |
| -for (int col = 0; col < board[0].length; ++col){ |
50 |
| -if (explore(board, word, row, col, 0)){ |
51 |
| -return true; |
52 |
| -} |
53 |
| -} |
54 |
| -} |
55 |
| -return false; |
56 |
| -} |
57 |
| -
|
58 |
| -// DFS method to explore the board and search for the word |
59 |
| -private boolean explore(char[][] board, String word, int row, int col, int index) { |
60 |
| -// Check if the current cell is out of bounds |
61 |
| -if (row < 0 || row == board.length || col < 0 || col == board[0].length){ |
62 |
| -return false; |
63 |
| -} |
64 |
| -
|
65 |
| -// Checking if the current cell matches the character of the word |
66 |
| -if (board[row][col] != word.charAt(index) || board[row][col] == '*'){ |
67 |
| -return false; |
68 |
| -} |
69 |
| -
|
70 |
| -// Checking if the entire word has been found |
71 |
| -if (index == word.length() - 1){ |
72 |
| -return true; |
73 |
| -} |
74 |
| -
|
75 |
| -// Temporarily marking the current cell as visited |
76 |
| -final char temp = board[row][col]; |
77 |
| -board[row][col] = '*'; |
78 |
| -
|
79 |
| -// Recursively exploring adjacent cells to find the next character of the word |
80 |
| -final boolean isFound = explore(board, word, row + 1, col, index + 1) || |
81 |
| -explore(board, word, row - 1, col, index + 1) || |
82 |
| -explore(board, word, row, col + 1, index + 1) || |
83 |
| -explore(board, word, row, col - 1, index + 1); |
| 42 | +public int maxDepth(String s) { |
84 | 43 |
|
85 |
| -// Backtrack by restoring the original value of the current cell |
86 |
| -board[row][col] = temp; |
| 44 | +// Initialize variables |
| 45 | +int jawab = 0; // Store the maximum depth found so far |
| 46 | +int khula = 0; // Keep track of the current depth |
87 | 47 |
|
88 |
| -return isFound; |
89 |
| -} |
| 48 | +// Iterate through each character in the string |
| 49 | +for (final char c : s.toCharArray()){ |
| 50 | +// If the character is an opening parenthesis |
| 51 | +if (c == '('){ |
| 52 | +khula++; // Incrementing the current depth |
| 53 | +jawab = Math.max(jawab, khula); // Updating maximum depth if necessary |
| 54 | +} |
| 55 | +// If the character is a closing parenthesis |
| 56 | +else if (c == ')'){ |
| 57 | +khula--; // Decrementing the current depth |
| 58 | +} |
| 59 | +} |
| 60 | +// Returning the maximum depth found |
| 61 | +return jawab; |
| 62 | +} |
90 | 63 | }
|
91 | 64 | ```
|
0 commit comments