Code Lab

Режим собеседования. Таймер на 15 мин, без подсказок и автодополнения

Balanced brackets

СложнаяСтроки15 мин

Write a function isBalanced(text) that returns true when the brackets in the string text are balanced, and false otherwise.

  • There are three kinds of brackets: (), [] and {}. Every other character is skipped: 'if (a[0]) { b() }' is balanced.
  • Every opening bracket needs a closing bracket of the same kind after it: '(]' is not balanced.
  • Brackets close in the reverse order of opening: '([)]' is not balanced, '([])' is.
  • A closing bracket with no open bracket before it makes the text not balanced: ')(' is not balanced.
  • A string with no brackets, also an empty string, is balanced.
  • The text can be long, up to 100 000 characters, and brackets can nest 50 000 levels deep. Your code must finish in time.

Примеры

isBalanced('([]{})')

Вернёт true

isBalanced('([)]')

Вернёт false

isBalanced('if (a[0]) { b() }')

Вернёт true

Спрашивали в Swiggy · Источник

Загружаем редактор...
  1. Входные данныеisBalanced('([]{})')
    Ожидалосьtrue
  2. Входные данныеisBalanced('([)]')
    Ожидалосьfalse
  3. Входные данныеisBalanced('if (a[0]) { b() }')
    Ожидалосьtrue

Скрытые тесты запускаются по Submit