File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
## Today's 07-04-24 [Problem Link](https://leetcode.com/problems/valid-parenthesis-string/description/?envType=daily-question&envId=2024-04-07)
2+
## 678. Valid Parenthesis String
3+
4+
# Intuition
5+
<!-- Describe your first thoughts on how to solve this problem. -->
6+
My algorithm aims to determine whether a given string containing parentheses, '*' (wildcards), and other characters forms a valid combination of parentheses. In a valid combination, each open parenthesis '(' must have a corresponding closing parenthesis ')', and wildcards '*' can be either an open parenthesis, a closing parenthesis, or an empty string.
7+
8+
# Approach
9+
<!-- Describe your approach to solving the problem. -->
10+
11+
- I iterated through each character in the string and maintain two counts :
12+
- `minOpen`: Represents the minimum count of open parentheses that must be closed.
13+
- `maxOpen`: Represents the maximum count of open parentheses that could be closed.
14+
15+
- I traversed the string character by character:
16+
- If I encountered an open parenthesis '(', both `minOpen` and `maxOpen` are incremented.
17+
- If I encountered a closing parenthesis ')', I decrement `minOpen` (ensuring it doesn't go below 0) and decrement `maxOpen`.
18+
- If I encountered a wildcard '*', I decrement `minOpen` (ensuring it doesn't go below 0) and increment `maxOpen` because it can act as an open parenthesis, closing parenthesis, or an empty string.
19+
20+
- At any point during the iteration, if the count of open parentheses (`maxOpen`) became negative, it means I have encountered more closing parentheses than open ones, which makes the string invalid, so I return `false`.
21+
22+
- Finally, I checked if all open parentheses are closed by verifying if `minOpen` is back to 0. If it is, then the string is valid; otherwise, it's invalid.
23+
24+
- My algorithm returned `true` if the string is valid and `false` otherwise.
25+
26+
---
27+
Have a look at the code , still have any confusion then please let me know in the comments
28+
Keep Solving.:)
29+
30+
# Complexity
31+
- Time complexity : $O(n)$
32+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
33+
$n$ : length of the input string
34+
- Space complexity : $O(1)$
35+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
36+
37+
# Code
38+
```
39+
class Solution {
40+
public boolean checkValidString(String str) {
41+
42+
// Initializing the minimum and maximum counts of open parentheses
43+
int minOpen = 0; // minimum count of open parentheses
44+
int maxOpen = 0; // maximum count of open parentheses
45+
46+
// Iterating through each character in the string
47+
for (char ch : str.toCharArray()) {
48+
// Handling different cases based on the character
49+
switch (ch) {
50+
51+
case '(':
52+
// Incrementing both minimum and maximum counts for open parentheses
53+
minOpen++;
54+
maxOpen++;
55+
break;
56+
case ')':
57+
// Decreasing the minimum count, but ensure it doesn't go negative
58+
minOpen = Math.max(0, --minOpen);
59+
// Decreasing the maximum count for open parentheses
60+
maxOpen--;
61+
break;
62+
case '*':
63+
// Decreasing the minimum count, but ensure it doesn't go negative
64+
minOpen = Math.max(0, --minOpen);
65+
// Incrementing the maximum count for open parentheses
66+
maxOpen++;
67+
break;
68+
}
69+
70+
// If the maximum count of open parentheses becomes negative, return false
71+
if (maxOpen < 0){
72+
return false;
73+
}
74+
}
75+
76+
// Checking if all open parentheses are closed
77+
return minOpen == 0;
78+
}
79+
}
80+
```

0 commit comments

Comments
 (0)