|
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 | 1 | # Leetcode Daily Challenge Solutions
|
8 | 2 |
|
9 | 3 | 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.
|
10 | 4 |
|
11 | 5 | ## Always here to assist you guys.
|
12 | 6 |
|
13 |
| -## 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) |
14 |
| -## 1614. Maximum Nesting Depth of the Parentheses |
| 7 | +## Today's 05-04-24 [Problem Link](https://leetcode.com/problems/make-the-string-great/description/?envType=daily-question&envId=2024-04-05) |
| 8 | +## 1544. Make The String Great |
15 | 9 |
|
16 | 10 | # Intuition
|
17 | 11 | <!-- Describe your first thoughts on how to solve this problem. -->
|
18 |
| -- I aim to find the maximum depth of nested parentheses in a given string. |
| 12 | +This problem requires removing pairs of adjacent characters from the input string if they have the same letter but different cases. |
| 13 | + |
| 14 | +To solve the problem, I can use a stack (implemented as a linked list) to keep track of the characters that have not been removed. |
| 15 | + |
19 | 16 | # Approach
|
20 | 17 | <!-- Describe your approach to solving the problem. -->
|
21 |
| -- I initialized two variables, `jawab` and `khula`, to keep track of the maximum depth and current depth, respectively. |
22 |
| -- Iterated through each character `c` in the string `s`. |
23 |
| -- If `c` is an opening parenthesis '(', incremented `khula` and update `jawab` to the maximum of its current value and `khula`. |
24 |
| -- If `c` is a closing parenthesis ')', decremented `khula`. |
25 |
| -- Finally, returned `jawab` as the maximum depth of parentheses. |
| 18 | +- I iterated through each character in the input string. |
| 19 | +- For each character : |
| 20 | +- If the stack is not empty and the current character, when compared with the last character in the stack, forms a pair of characters with the same letter but different cases, I removed the last character from the stack. |
| 21 | +- If the current character and the next character (if exists) form a pair of characters with the same letter but different cases, I skipped the next character. |
| 22 | +- Otherwise, I added the current character to the stack. |
| 23 | +- After processing all characters, I converted the characters remaining in the stack to a string, which represents the final result. |
| 24 | +- Finally, I returned the final result string. |
26 | 25 |
|
27 | 26 | ---
|
28 | 27 | Have a look at the code , still have any confusion then please let me know in the comments
|
29 | 28 | Keep Solving.:)
|
| 29 | + |
30 | 30 | # Complexity
|
31 | 31 | - Time complexity : $O(n)$
|
32 | 32 | <!-- Add your time complexity here, e.g. $$O(n)$$ -->
|
33 |
| -$n$ : length of the given string |
34 |
| -- Space complexity : $O(1)$ |
| 33 | +$n$ : length of the input string |
| 34 | +- Space complexity : $O(n)$ |
35 | 35 | <!-- Add your space complexity here, e.g. $$O(n)$$ -->
|
36 | 36 |
|
37 | 37 | # Code
|
38 | 38 | ```
|
39 | 39 | class Solution {
|
40 |
| -public int maxDepth(String s) { |
41 |
| -
|
42 |
| -// Initialize variables |
43 |
| -int jawab = 0; // Store the maximum depth found so far |
44 |
| -int khula = 0; // Keep track of the current depth |
45 |
| -
|
46 |
| -// Iterate through each character in the string |
47 |
| -for (final char c : s.toCharArray()){ |
48 |
| -// If the character is an opening parenthesis |
49 |
| -if (c == '('){ |
50 |
| -khula++; // Incrementing the current depth |
51 |
| -jawab = Math.max(jawab, khula); // Updating maximum depth if necessary |
52 |
| -} |
53 |
| -// If the character is a closing parenthesis |
54 |
| -else if (c == ')'){ |
55 |
| -khula--; // Decrementing the current depth |
| 40 | +
|
| 41 | +public String makeGood(String s) { |
| 42 | +
|
| 43 | +// Converting the input string to a character array for easier manipulation. |
| 44 | +char[] chArr = s.toCharArray(); |
| 45 | +
|
| 46 | +// Initializing an empty string 'jawab' to store the final result. |
| 47 | +String jawab=""; |
| 48 | +
|
| 49 | +// Initializing a linked list to store characters. |
| 50 | +LinkedList<Character> linkedList = new LinkedList<>(); |
| 51 | +
|
| 52 | +// Looping through each character in the input string. |
| 53 | +for(int i = 0; i < chArr.length; i++){ |
| 54 | +// If the linked list is not empty and the absolute difference between the last character in the linked list and the current character is equal to the difference between 'a' and 'A' (which represents the difference between lowercase and uppercase characters), |
| 55 | +// Removing the last character from the linked list and continue to the next iteration. |
| 56 | +if(!linkedList.isEmpty() && Math.abs(linkedList.getLast() - chArr[i]) == 'a' - 'A'){ |
| 57 | +linkedList.removeLast(); |
| 58 | +continue; |
56 | 59 | }
|
| 60 | +
|
| 61 | +// If the current character and the next character (if exists) form a pair of characters with the same letter but different cases, |
| 62 | +// Skipping the next character and continue to the next iteration. |
| 63 | +if(i + 1 != chArr.length && Math.abs(chArr[i + 1] - chArr[i]) == 'a' - 'A') |
| 64 | +i++; |
| 65 | +else |
| 66 | +// Otherwise, adding the current character to the linked list. |
| 67 | +linkedList.add(chArr[i]); |
57 | 68 | }
|
58 |
| -// Returning the maximum depth found |
| 69 | +
|
| 70 | +// Converting the characters remaining in the linked list to a string and append them to the 'jawab' string. |
| 71 | +while(!linkedList.isEmpty()){ |
| 72 | +jawab+=(linkedList.removeFirst()); |
| 73 | +} |
| 74 | +
|
| 75 | +// Returning the final result string. |
59 | 76 | return jawab;
|
60 | 77 | }
|
61 | 78 | }
|
62 |
| -``` |
| 79 | +``` |
0 commit comments