Skip to main content

2. Valid Parentheses

easyAsked at Riot Games

Validate balanced brackets with a stack — Riot uses this to gauge stack fluency before chat-protocol parsing questions.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

Given a string containing only '()[]{}', determine whether brackets are properly opened and closed in the correct order. Empty input is considered valid.

Constraints

  • 1 <= s.length <= 10^4
  • s consists of bracket characters only

Examples

Example 1

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

Example 2

Input
s="(]"
Output
false

Approaches

1. Repeated replace

Strip () [] {} pairs until the string stops shrinking.

Time
O(n^2)
Space
O(n)
while (/\(\)|\[\]|\{\}/.test(s)) s = s.replace(/\(\)|\[\]|\{\}/g, '');
return s === '';

Tradeoff:

2. Stack

Push openers, on closers pop and verify the match.

Time
O(n)
Space
O(n)
function isValid(s) {
  const pair = { ')': '(', ']': '[', '}': '{' };
  const st = [];
  for (const c of s) {
    if ('([{'.includes(c)) st.push(c);
    else if (st.pop() !== pair[c]) return false;
  }
  return st.length === 0;
}

Tradeoff:

Riot Games-specific tips

At Riot the stack pattern recurs in chat-protocol tokenizers and replay-log parsers where you must reject malformed frames before they reach the server tick loop.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

Drill Valid Parentheses and other Riot Games interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →