LeetCode: Score Of Parentheses Solution
1/**2 * @param {string} S3 * @return {number}4 */5var scoreOfParentheses = function (S) {6 const resStack = [0] // default for empty string78 for (const paren of S) {9 if (paren === "(") {10 resStack.push(0)11 } else if (paren === ")") {12 const top = resStack.pop()13 const res = top > 0 ? top * 2 : 114 resStack[resStack.length - 1] += res15 }16 }1718 return resStack.pop()19}
Comments
Loading comments...
Tags
leetcode
string
stack
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: Shortest Unsorted Continuous Subarray
Feb 25, 2021
Previous Post
LeetCode: Search A 2d Matrix II
Feb 23, 2021