
Problem
Solution
Submissions
Valid Parentheses
Certification: Basic Level
Accuracy: 50%
Submissions: 66
Points: 5
Write a Java program to implement the isValid(String s) function, which determines if an input string containing just the characters '(', ')', '{', '}', '[' and ']' is valid.
Example 1
- Input: s = "()"
- Output: true
- Explanation:
- Push '(' → Stack = ['(']
- Encounter ')' → Matches top, pop '('
- Stack is empty → valid
Example 2
- Input: s = "([)]"
- Output: false
- Explanation:
- Push '(' → Push '['
- ')' does not match top '[' → invalid
Constraints
- 1 ≤ s.length ≤ 10^4
- s consists of parentheses only '()[]{}'
- Time Complexity: O(n)
- Space Complexity: O(n)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use a stack to keep track of opening brackets
- When you encounter an opening bracket, push it onto the stack
- When you encounter a closing bracket, check if the stack is empty or if the top element is the corresponding opening bracket
- If the stack is not empty at the end, the string is invalid