Codility: Nesting Solution

Lesson 7 Stacks and Queues
1function 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.length
16 },
17 }
18}
19
20function solution(S) {
21 const chars = S.split("")
22 const stack = new Stack()
23 chars.forEach(c => {
24 if (c === "(") {
25 stack.push(c)
26 } else {
27 const head = stack.pop()
28 if (head === "(") {
29 return
30 }
31 stack.push(c)
32 }
33 })
34 return stack.size === 0 ? 1 : 0
35}

Comments

Loading comments...

Tags

codility

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

Codility: Fish

Lesson 7 Stacks and Queues

Previous Post

Codility: StoneWall

Lesson 7 Stacks and Queues

HoningJS

Search Posts