# Balanced Brackets

![](https://3743232000-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHW2IQuh2PFpWJDvBz2FF%2Fuploads%2Fgit-blob-912adfee0c1aa8d3568c62ccee8ab16e3ab8c8f7%2FScreenshot%202023-02-04%20at%2016.10.55.png?alt=media)

* 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;
```
