GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • LeetCodeJavascript.com
  • Star on GitHub

This view

  • Original page
  • New address

GeezerVison is showing leetcodejavascript.com — #1190 - Reverse Substrings Between Each Pair of Parentheses - LeetCode JavaScript Solutions

Back to all solutions

#1190 - Reverse Substrings Between Each Pair of Parentheses

Medium • View on LeetCode • View on GitHub
String Stack

Problem Description

You are given a string s that consists of lower case English letters and brackets.

Reverse the strings in each pair of matching parentheses, starting from the innermost one.

Your result should not contain any brackets.

Solution

/**
 * @param {string} s
 * @return {string}
 */
var reverseParentheses = function(s) {
  const stack = [[]];

  for (const char of s) {
    if (char === '(') {
      stack.push([]);
    } else if (char === ')') {
      const reversed = stack.pop().reverse();
      stack[stack.length - 1].push(...reversed);
    } else {
      stack[stack.length - 1].push(char);
    }
  }

  return stack[0].join('');
};
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.