Files
freeCodeCamp/curriculum/challenges/russian/08-coding-interview-prep/rosetta-code/balanced-brackets.russian.md

4.0 KiB

title, id, challengeType, forumTopicId, localeTitle
title id challengeType forumTopicId localeTitle
Balanced brackets 594dc6c729e5700999302b45 5 302230 Сбалансированные кронштейны

Description

Определите, сбалансирована ли сгенерированная строка скобок; то есть, состоит ли он целиком из пар открывающих / закрывающих скобок (в этом порядке), ни одно из которых не выполняется.

Примеры:

(пусто) true

[] true

][ false

[][] true

][][ false

[]][[] false

[[[[]]]] true

Instructions

Tests

tests:
  - text: <code>isBalanced</code> is a function.
    testString: assert(typeof isBalanced === 'function');
  - text: <code>isBalanced("[]")</code> should return true.
    testString: assert(isBalanced(testCases[0]));
  - text: <code>isBalanced("]][[[][][][]][")</code> should return false.
    testString: assert(!isBalanced(testCases[1]));
  - text: <code>isBalanced("[][[[[][][[[]]]]]]")</code> should return true.
    testString: assert(isBalanced(testCases[2]));
  - text: <code>isBalanced("][")</code> should return true.
    testString: assert(!isBalanced(testCases[3]));
  - text: <code>isBalanced("[[[]]]][[]")</code> should return true.
    testString: assert(!isBalanced(testCases[4]));
  - text: <code>isBalanced("][[]")</code> should return true.
    testString: assert(!isBalanced(testCases[5]));
  - text: <code>isBalanced("][[][]][[[]]")</code> should return true.
    testString: assert(!isBalanced(testCases[6]));
  - text: <code>isBalanced("[[][]]][")</code> should return true.
    testString: assert(!isBalanced(testCases[7]));
  - text: <code>isBalanced("[[[]]][[]]]][][[")</code> should return true.
    testString: assert(!isBalanced(testCases[8]));
  - text: <code>isBalanced("[]][[]]][[[[][]]")</code> should return true.
    testString: assert(!isBalanced(testCases[9]));
  - text: <code>isBalanced("][]][[][")</code> should return true.
    testString: assert(!isBalanced(testCases[10]));
  - text: <code>isBalanced("[[]][[][]]")</code> should return true.
    testString: assert(isBalanced(testCases[11]));
  - text: <code>isBalanced("[[]]")</code> should return true.
    testString: assert(isBalanced(testCases[12]));
  - text: <code>isBalanced("]][]][[]][[[")</code> should return true.
    testString: assert(!isBalanced(testCases[13]));
  - text: <code>isBalanced("][]][][[")</code> should return true.
    testString: assert(!isBalanced(testCases[14]));
  - text: <code>isBalanced("][][")</code> should return true.
    testString: assert(!isBalanced(testCases[15]));
  - text: <code>isBalanced("[[]]][][][[]][")</code> should return true.
    testString: assert(!isBalanced(testCases[16]));
  - text: <code>isBalanced("")</code> should return true.
    testString: assert(isBalanced(testCases[17]));

Challenge Seed

function isBalanced(str) {
  // Good luck!
  return true;
}

After Tests

const testCases = [
  '[]',
  ']][[[][][][]][',
  '[][[[[][][[[]]]]]]',
  '][',
  '[[[]]]][[]',
  '][[]',
  '][[][]][[[]]',
  '[[][]]][',
  '[[[]]][[]]]][][[',
  '[]][[]]][[[[][]]',
  '][]][[][',
  '[[]][[][]]',
  '[[]]',
  ']][]][[]][[[',
  '][]][][[',
  '][][',
  '[[]]][][][[]][',
  ''
];

Solution

function isBalanced(str) {
  if (str === '') return true;
  let a = str;
  let b;
  do {
    b = a;
    a = a.replace(/\[\]/g, '');
  } while (a !== b);
  return !a;
}