The leetcode problem:“20. Valid Parentheses Attempted Easy Topics Companies Hint Given a…
- User
The leetcode problem:“20. Valid Parentheses Attempted Easy Topics Companies Hint Given a string s containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Every close bracket has a corresponding open bracket of the same type.
Example 1:
Input: s = “()” Output: true Example 2:
Input: s = “()[]{}” Output: true Example 3:
Input: s = “(]” Output: false
Constraints:
1 <= s.length <= 104 s consists of parentheses only ‘()[]{}’.” write the code of the test cases
- Assistant
To validate the code for the "Valid Parentheses" problem, we need to write test cases for it. Below is the Python code for the function that checks if the given string of parentheses is valid, along with some test cases.
Python Function to Validate Parentheses
def is_valid_parentheses(s): stack = [] mapping = {')': '(', '}': '{', ']': '['} for char in s: if char in mapping: top_element = stack.pop() if stack else '#' if mapping[char] != top_element: return False else: stack.append(char) return not stack # Test Cases test_cases = [ {"input": "()", "expected": True}, {"input": "()[]{}", "expected": True}, {"input": "(]", "expected": False}, {"input": "([{}])", "expected": True}, {"input": "((()))", "expected": True}, {"input": "((())", "expected": False}, {"input": "", "expected": True}, {"input": "([)]", "expected": False}, {"input": "{[]}", "expected": True}, {"input": "[({]})", "expected": False} ] # Running Test Cases for i, test_case in enumerate(test_cases, 1): input_str = test_case["input"] expected_output = test_case["expected"] assert is_valid_parentheses(input_str) == expected_output, f"Test case {i} failed: input({input_str})" print("All test cases passed!")Explanation of Test Cases:
- Test Case 1:
- Input:
"()" - Expected Output:
True - This is a simple case with matched brackets.
- Input:
- Test Case 2:
- Input:
"()[]{}" - Expected Output:
True - This case checks multiple types of matched brackets.
- Input:
- Test Case 3:
- Input:
"(]" - Expected Output:
False - This case has mismatched brackets.
- Input:
- Test Case 4:
- Input:
"([{}])" - Expected Output:
True - This case has correctly nested brackets.
- Input:
- Test Case 5:
- Input:
"((()))" - Expected Output:
True - This case has nested and sequentially matched brackets.
- Input:
- Test Case 6:
- Input:
"((())" - Expected Output:
False - This case is missing a closing bracket.
- Input:
- Test Case 7:
- Input:
"" - Expected Output:
True - An empty string is trivially valid.
- Input:
- Test Case 8:
- Input:
"([)]" - Expected Output:
False - This case has mixed but incorrect order of brackets.
- Input:
- Test Case 9:
- Input:
"{[]}" - Expected Output:
True - This case has matched and correctly nested brackets.
- Input:
- Test Case 10:
- Input:
"[({]})" - Expected Output:
False - This case starts nested but goes wrong.
- Input:
Execution of Test Cases:
In the above code, the
assertstatement validates each test case. If any test case fails, it will raise anAssertionErrorwith a message indicating the failed test case. If all test cases pass, it will print "All test cases passed!".You can run this code in your Python environment to verify the implementation and ensure that all test cases are handled as expected.
- Test Case 1: