Leetcode 678: Valid Parenthesis String
Given a string consisting of ‘(’, ‘)’, and ‘’, determine if the string is valid according to the following rules. ‘’ can be treated as a left parenthesis, a right parenthesis, or an empty string.
Problem
Approach
Steps
Complexity
Input: The input is a string containing only '(', ')', and '*' characters.
Example: "(*)"
Constraints:
• 1 <= s.length <= 100
• s[i] is either '(', ')', or '*'
Output: The output should be true if the string is valid, otherwise false.
Example: true
Constraints:
• The output will be a boolean value, either true or false.
Goal: Efficiently validate the string using a stack to account for both parentheses and '*' characters.
Steps:
• 1. Traverse the string character by character.
• 2. Use two stacks: one for tracking '(' characters and one for tracking '*' characters.
• 3. If a ')' is encountered, check if there is a matching '(' or '*' to balance it.
• 4. After traversal, check if all '(' have corresponding ')' or '*' to balance them.
Goal: The constraints ensure that the problem remains manageable within time and space limits.
Steps:
• The length of the string s is between 1 and 100.
• The string contains only the characters '(', ')', and '*'.
Assumptions:
• All parentheses are balanced by either another parenthesis or a '*' character.
• Input: "(*)"
• Explanation: In this example, '*' can be treated as either a '(' or a ')', making the string valid.
• Input: "(*))"
• Explanation: The string is valid because '*' can be treated as '(', and the remaining parentheses are balanced.
Approach: We can use two stacks to track the positions of parentheses and '*' characters, ensuring the string remains valid.
Observations:
• We need to ensure that the parentheses are properly balanced, with '*' acting as a wildcard.
• Using two stacks will allow us to handle matching of both '(' and ')' while also utilizing '*' as a flexible placeholder.
Steps:
• 1. Iterate through the string to check for '(' and ')'.
• 2. Track positions of '(' and '*' using stacks.
• 3. For each ')', attempt to match it with a '(' from the stack. If none is available, use '*' if possible.
• 4. After the loop, check if there are any unmatched '(' characters left.
Empty Inputs:
• An empty string is considered valid.
Large Inputs:
• The string length can be up to 100, which is manageable within the time constraints.
Special Values:
• A string consisting only of '*' characters is valid because '*' can be treated as empty, '(' or ')'.
Constraints:
• Ensure efficient handling of edge cases with '*' characters.
bool checkValidString(string s) {
stack<int> stk, str;
int cnt = 0;
for(int i = 0; i < s.size(); i++) {
if(s[i] == '*')
str.push(i);
else if(s[i] == '(')
stk.push(i);
else {
if(!stk.empty())
stk.pop();
else if(!str.empty()) str.pop();
else return false;
}
}
while(!stk.empty() && !str.empty() && stk.top() < str.top()) {
str.pop();
stk.pop();
}
return stk.empty();
}
1 : Method Definition
bool checkValidString(string s) {
Define the method 'checkValidString' that takes a string s and checks whether it contains a valid combination of parentheses and stars.
2 : Container Initialization
stack<int> stk, str;
Initialize two stacks: one for holding indices of '(' (stk) and another for holding indices of '*' (str).
3 : Variable Initialization
int cnt = 0;
Initialize a counter variable (cnt), although it's not used in the current implementation.
4 : Loop
for(int i = 0; i < s.size(); i++) {
Start a loop to iterate through each character of the string s.
5 : Condition
if(s[i] == '*')
Check if the current character is a '*' character.
6 : Stack Operation
str.push(i);
If the character is '*', push its index onto the 'str' stack.
7 : Condition
else if(s[i] == '(')
Check if the current character is an opening parenthesis '('.
8 : Stack Operation
stk.push(i);
If the character is '(', push its index onto the 'stk' stack.
9 : Else Block
else {
Else, when the character is a closing parenthesis ')'.
10 : Condition
if(!stk.empty())
Check if the 'stk' stack is not empty, indicating there is an unmatched '(' available.
11 : Stack Operation
stk.pop();
Pop the top element of the 'stk' stack, matching this ')' with an earlier '('.
12 : Condition
else if(!str.empty()) str.pop();
If the 'stk' stack is empty, check if there is an unmatched '*' character. Pop the '*' from the 'str' stack.
13 : Return
else return false;
If neither '(' nor '*' are available to match with the closing ')', return false indicating an invalid string.
14 : Block End
}
End of the 'else' block for processing closing parentheses.
15 : Loop
while(!stk.empty() && !str.empty() && stk.top() < str.top()) {
Use a while loop to match any remaining '(' in the 'stk' stack with '*' in the 'str' stack.
16 : Stack Operation
str.pop();
Pop the top element of the 'str' stack to match it with the remaining '(' in the 'stk' stack.
17 : Stack Operation
stk.pop();
Pop the top element of the 'stk' stack, effectively matching this '(' with a '*'.
18 : Return
return stk.empty();
Return true if the 'stk' stack is empty (all '(' have been matched) or false if any unmatched '(' remain.
Best Case: O(n)
Average Case: O(n)
Worst Case: O(n)
Description: Both the best and worst case scenarios involve traversing the string once, making the time complexity O(n).
Best Case: O(n)
Worst Case: O(n)
Description: In the worst case, we may need to store all the parentheses in the stack, which takes O(n) space.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus