|
| 1 | +## Today's 06*04=24 [Problem Link](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/description/?envType=daily-question&envId=2024-04-06) |
| 2 | +## 1249. Minimum Remove to Make Valid Parentheses |
| 3 | + |
| 4 | +# Intuition |
| 5 | +<!-- Describe your first thoughts on how to solve this problem. --> |
| 6 | +To address this problem, I utilize a stack to manage the indices of unpaired '(' characters. I traverse the string, marking unpaired ')' characters as '#' and popping indices from the stack for each encountered ')', effectively pairing '(' and ')'. Any remaining unpaired '(' characters are marked as '#' after processing. Finally, I remove all '#' characters to obtain the valid string. |
| 7 | + |
| 8 | +# Approach |
| 9 | +<!-- Describe your approach to solving the problem. --> |
| 10 | +**Initialized** an empty stack (`parenthesesStack`) to store indices of unpaired '(' characters. |
| 11 | + |
| 12 | +**Traversed** the input string character by character: |
| 13 | + - If the current character is '(', push its index onto `parenthesesStack`. |
| 14 | + - If the current character is ')': |
| 15 | + - If `parenthesesStack` is empty, marked the ')' character as '#'. |
| 16 | + - If `parenthesesStack` is not empty, popped the top index to pair '(' and ')'. |
| 17 | + |
| 18 | +**Marked** any remaining unpaired '(' characters as '#' by popping from `parenthesesStack`. |
| 19 | + |
| 20 | +**Removed** all '#' characters from the modified string. |
| 21 | + |
| 22 | +**Returned** the resulting string, which now contains the minimum removed parentheses to make it valid. |
| 23 | + |
| 24 | +--- |
| 25 | +Have a look at the code , still have any confusion then please let me know in the comments |
| 26 | +Keep Solving.:) |
| 27 | + |
| 28 | +# Complexity |
| 29 | +- Time complexity : $O(s)$ |
| 30 | +<!-- Add your time complexity here, e.g. $$O(n)$$ --> |
| 31 | +$s$ : length of the input string |
| 32 | +- Space complexity : $O(s)$ |
| 33 | +<!-- Add your space complexity here, e.g. $$O(n)$$ --> |
| 34 | + |
| 35 | +# Code |
| 36 | +``` |
| 37 | +class Solution { |
| 38 | + public String minRemoveToMakeValid(String s) { |
| 39 | +
|
| 40 | + Deque<Integer> parenthesesStack = new ArrayDeque<>(); // Stores indices of unpaired '(' characters |
| 41 | + StringBuilder modifiedString = new StringBuilder(s); |
| 42 | +
|
| 43 | + for (int i = 0; i < s.length(); ++i) { |
| 44 | + if (modifiedString.charAt(i) == '(') { |
| 45 | + parenthesesStack.push(i); // Record the unpaired '(' index. |
| 46 | + } |
| 47 | + else if (modifiedString.charAt(i) == ')') { |
| 48 | + if (parenthesesStack.isEmpty()) { |
| 49 | + modifiedString.setCharAt(i, '#'); // Mark the unpaired ')' as '#'. |
| 50 | + } |
| 51 | + else { |
| 52 | + parenthesesStack.pop(); // Find a pair! |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +
|
| 57 | + // Marking the unpaired '(' as '#'. |
| 58 | + while (!parenthesesStack.isEmpty()) { |
| 59 | + modifiedString.setCharAt(parenthesesStack.pop(), '#'); |
| 60 | + } |
| 61 | +
|
| 62 | + return modifiedString.toString().replaceAll("#", ""); |
| 63 | + } |
| 64 | +} |
| 65 | +
|
| 66 | +``` |
0 commit comments