# Codility: Brackets Solution

Lesson 7 Stacks and Queues
```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}function Stack() {2  stack = []3
4  this.push = function (el) {5    stack.push(el)6  }7
8  this.pop = function () {9    return stack.pop()10  }11
12  return {13    ...this,14    get size() {15      return stack.length16    },17    get head() {18      return stack[this.size - 1]19    },20  }21}22
23function solution(S) {24  const isOpen = c => "{[(".includes(c)25  const closeOf = c =>26    ({27      "{": "}",28      "[": "]",29      "(": ")",30    }[c])31  const chars = S.split("")32  const stack = new Stack()33  chars.forEach(c => {34    if (isOpen(c)) {35      stack.push(c)36    } else if (c === closeOf(stack.head)) {37      stack.pop()38    } else {39      stack.push(c)40    }41  })42  return stack.size === 0 ? 1 : 043}```

codility

## Next Post

LeetCode: Kth Largest Element In An Array

Jan 24, 2021

## Previous Post

Codility: Fish

Jan 23, 2021

Lesson 7 Stacks and Queues

Search Posts