Files
freeCodeCamp/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/balanced-brackets.english.md
Randell Dawson c25916c9a2 fix(curriculum): changed test text to use should for Coding Interview Prep - part 2 of 2 (#37766)
* fix: changed test text to use should

* fix: corrected typo

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>

* fix: removed extra period

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>

* fix: removed extra period

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>

* fix: removed extra period

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>

* fix: removed extra period

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>

* fix: corrected typo

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>
2019-11-20 10:01:31 -05:00

3.6 KiB

title, id, challengeType, forumTopicId
title id challengeType forumTopicId
Balanced brackets 594dc6c729e5700999302b45 5 302230

Description

Determine whether a generated string of brackets is balanced; that is, whether it consists entirely of pairs of opening/closing brackets (in that order), none of which mis-nest.

Examples:

Input Output
[] true
][ false
[][] true
][][ false
[]][[] false
[[[[]]]] true

Instructions

Tests

tests:
  - text: <code>isBalanced</code> should be 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 false.
    testString: assert(!isBalanced(testCases[3]));
  - text: <code>isBalanced("[[[]]]][[]")</code> should return false.
    testString: assert(!isBalanced(testCases[4]));
  - text: <code>isBalanced("][[]")</code> should return false.
    testString: assert(!isBalanced(testCases[5]));
  - text: <code>isBalanced("][[][]][[[]]")</code> should return false.
    testString: assert(!isBalanced(testCases[6]));
  - text: <code>isBalanced("[[][]]][")</code> should return false.
    testString: assert(!isBalanced(testCases[7]));
  - text: <code>isBalanced("[[[]]][[]]]][][[")</code> should return false.
    testString: assert(!isBalanced(testCases[8]));
  - text: <code>isBalanced("[]][[]]][[[[][]]")</code> should return false.
    testString: assert(!isBalanced(testCases[9]));
  - text: <code>isBalanced("][]][[][")</code> should return false.
    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 false.
    testString: assert(!isBalanced(testCases[13]));
  - text: <code>isBalanced("][]][][[")</code> should return false.
    testString: assert(!isBalanced(testCases[14]));
  - text: <code>isBalanced("][][")</code> should return false.
    testString: assert(!isBalanced(testCases[15]));
  - text: <code>isBalanced("[]]]")</code> should return false.
    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 Test

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;
}