easy Valid Parentheses
Problem Statement
Determine if an input string containing only the characters '(', ')', '{', '}', '[', and ']' is valid. A string is considered valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
- Each close bracket has a corresponding open bracket of the same type.
Examples
-
- Input:
"(]" - Expected Output:
false - Justification: The opening parenthesis '(' is not closed by its corresponding closing parenthesis.
- Input:
-
- Input:
"{[]}" - Expected Output:
true - Justification: The string contains pairs of opening and closing brackets in the correct order.
- Input:
-
- Input:
"[{]}" - Expected Output:
false - Justification: The opening square bracket '[' is closed by a curly brace '}', which is incorrect.
- Input:
Constraints:
- 1 <= s.length <= 104
sconsists of parentheses only '()[]{}'.
Try it yourself
Try solving this question here:
✅ Solution Valid Parentheses
Problem Statement
Determine if an input string containing only the characters '(', ')', '{', '}', '[', and ']' is valid. A string is considered valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
- Each close bracket has a corresponding open bracket of the same type.
Examples
-
- Input:
"(]" - Expected Output:
false - Justification: The opening parenthesis '(' is not closed by its corresponding closing parenthesis.
- Input:
-
- Input:
"{[]}" - Expected Output:
true - Justification: The string contains pairs of opening and closing brackets in the correct order.
- Input:
-
- Input:
"[{]}" - Expected Output:
false - Justification: The opening square bracket '[' is closed by a curly brace '}', which is incorrect.
- Input:
Constraints:
- 1 <= s.length <= 104
sconsists of parentheses only '()[]{}'.
Solution
- Initialization:
- Start with an empty stack.
- For every bracket in the input string:
- If the bracket is an opening bracket ('(', '{', '['), push it onto the stack.
- If the bracket is a closing bracket (')', '}', ']'):
- If the stack is empty, return false.
- Pop the top of the stack and check if it matches the corresponding opening bracket.
- Validation:
- If the current closing bracket doesn't match the top of the stack, return false.
- If the stack is not empty after processing all brackets in the string, return false.
- Final Output:
- If the stack is empty at the end, return true, indicating that the string has valid parentheses.
Algorithm Walkthrough
Using the input "{[]}":
- Start with an empty stack.
- Encounter '{', push onto the stack.
- Encounter '[', push onto the stack.
- Encounter ']', pop from the stack and check if the popped character is '['. It matches, so continue.
- Encounter '}', pop from the stack and check if the popped character is '{'. It matches.
- The string is processed and the stack is empty, so return true.

Code
java
import java.util.Stack;
public class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
// Iterate through each character in the string
for (char c : s.toCharArray()) {
// If it's an opening bracket, push to stack
if (c == '(' || c == '{' || c == '[') {
stack.push(c);
} else {
// If stack is empty, it means no matching opening bracket
if (stack.isEmpty()) return false;
char top = stack.pop(); // Get the top element of the stack
// Check if the current closing bracket matches the top of the stack
if (c == ')' && top != '(') return false;
if (c == '}' && top != '{') return false;
if (c == ']' && top != '[') return false;
}
}
// If stack is empty, all brackets were matched
return stack.isEmpty();
}
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.isValid("(]")); // false
System.out.println(solution.isValid("{[]}")); // true
System.out.println(solution.isValid("[{]}")); // false
}
}
Complexity Analysis
- Time Complexity: O(n) where n is the length of the string. Each character is processed once.
- Space Complexity: O(n) in the worst case when all characters are opening brackets.
🤖 Don't fully get this? Learn it with Claude
Stuck on Valid Parentheses? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
🪜 Hint ladder (no spoilers)
Progressively stronger hints — you still solve it.
I'm working on the problem **Valid Parentheses** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🎨 Explain the approach visually
See the technique, not just code.
Explain the optimal approach to **Valid Parentheses** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔍 Review my solution
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Valid Parentheses**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔁 Drill the pattern
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Valid Parentheses**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.