Sitemap

Member-only story

Day 25 — Valid Parentheses | Blind 75 | Java

1 min readAug 23, 2026

--

Question

Given a string s containing only ()[]{}, determine if the string is valid.

A string is valid if:

  • Every opening bracket has a matching closing bracket.
  • Brackets are closed in the correct order.
  • Brackets match the correct type.

Input & Output

Input:  s = "()"
Output: true
Input:  s = "()[]{}"
Output: true
Input:  s = "(]"
Output: false
Input:  s = "([)]"
Output: false
Input:  s = "([])"
Output: true

Approach

We use a Stack because brackets follow Last In, First Out (LIFO).

  • If we see an opening bracket, push it into the stack.
  • If we see a closing bracket, check whether it matches the top of the stack.
  • If it doesn’t match, return false.
  • At the end, the stack must be empty.

We use a HashMap to store the matching pairs:

) → (
} → {
] → [

Code

import java.util.*;

class Solution {
public boolean isValid(String s) {
Deque<Character> stack = new ArrayDeque<>();
Map<Character, Character> resultMap = new HashMap<>();
resultMap.put(')', '(');
resultMap.put('}', '{')…

--

--

Priyanka
Priyanka

Written by Priyanka

Software developer sharing practical insights on development & modern technology. Writing about concepts, lessons learned & real-world development experiences.