LeetCode: Evaluate The Bracket Pairs Of A String Solution

1/**
2 * @param {string} s
3 * @param {string[][]} knowledge
4 * @return {string}
5 */
6var evaluate = function (s, knowledge) {
7 const dict = knowledge.reduce(
8 (acc, el) => acc.set(`(${el[0]})`, el[1]),
9 new Map()
10 )
11 const N = s.length
12 let tokens = []
13 let token = ""
14 for (let i = 0; i < N; i++) {
15 if (s[i] === "(") {
16 tokens.push(token)
17 token = ""
18 }
19 token += s[i]
20 if (s[i] === ")") {
21 tokens.push(token)
22 token = ""
23 }
24 }
25 if (token) {
26 tokens.push(token)
27 }
28
29 return tokens
30 .filter(Boolean)
31 .map(token => (token[0] === "(" ? dict.get(token) || "?" : token))
32 .join("")
33}

Comments

Loading comments...

Tags

leetcode

hash table

string

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

LeetCode: Minimum Number Of Operations To Reinitialize A Permutation

Mar 28, 2021

Previous Post

HoningJS

Search Posts