> For the complete documentation index, see [llms.txt](https://algorithm.prettylog.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://algorithm.prettylog.com/algorithm-problems/algoexpert/medium/balanced-brackets.md).

# Balanced Brackets

![](/files/ZuciZSBA2jbyD0Bnat76)

* n, n

```tsx
// ()

const map = {
  ')': '(',
  '}': '{',
  ']': '['
};

const set = new Set([')', '(', '}', '{', ']', '[']);

function balancedBrackets(str) {

  const stack = [];

  for (let i = 0; i < str.length; i++) {
    const curr = str[i];

    if (!set.has(curr)) continue;
    
    if (curr === '(' || curr === '{' || curr === '[') {
      stack.push(curr);
      continue;
    }

    const top = stack.pop();
    if (top === undefined) return false;
    if (map[curr] !== top) return false;
    
  }

  if (stack.length > 0) return false;

  return true;

}

// Do not edit the line below.
exports.balancedBrackets = balancedBrackets;
```
