I'm having a really tough time solving this problem with JavaScript
You are given a string s that consists of English letters, punctuation marks, whitespace characters and brackets. It is guaranteed that the brackets in s form a regular bracket sequence.
Your task is to reverse the strings in each pair of matching parenthesis, starting from the innermost one.
Example
For string "s = a(bc)de" the output should be
reverseParentheses(s) = "acbde".
Input/Output
[time limit] 4000ms (js) [input] string s
A string consisting of English letters, punctuation marks, whitespace characters and brackets. It is guaranteed that parenthesis form a regular bracket sequence.
Constraints:
5 ≤ x.length ≤ 55.
[output] string
It has to work with the following inputs:
- s: "a(bcdefghijkl(mno)p)q" Expected Output: "apmnolkjihgfedcbq"
- s: "co(de(fight)s)" Expected Output: "cosfighted"
In JS
Using Regex
Simple Method:-
In Python:
Simple Method
Using Stacks Method
In C++
Simple Method:-
reverseString
function will reverse the String using the swapping method whilereverseParentheses
function will update string recursively.A solution in F#:
Given a string of size n, here's a recursion code written in C which runs in O(n) time complexity. The idea behind the code is to start with the beginning of the string and every time you encounter an opening bracket, you switch to its closing bracket and print backwards then complete printing after that closing brackets. Note that when you are printing backwards, opening brackets '[' are considered closing brackets and vise versa for closing brackets ']'. Maximum string size is 1 million, change array sizes if you need to process longer strings.