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:
a(bcdefghijkl(mno)p)q
Expected Output:
apmnolkjihgfedcbq
co(de(fight)s)
Expected Output: cosfighted
function reverseParentheses(s) {
if (s.includes('(')){
return reverseParentheses(reverseOnce(s));
} else {
return s;
}
}
function reverseOnce(s){
var regexp = /\(([^()]*)\)/i;
var subStr = regexp.exec(s)[1];
subStr = subStr.split('').reverse().join('');
return s.replace(regexp, subStr)
}
In JS
Using Regex
function reverseInParentheses(s) {
if (s.match(/\([a-z]*\)/)) {
return reverseInParentheses(s.replace(/\([a-z]*\)/,
Array.from(s.match(/\([a-z]*\)/)[0].replace(/\(|\)/g,'')).reverse().join('')));
}
else return s;
}
Simple Method:-
function reverseInParentheses(s) {
while (true) {
let c = s.indexOf(")");
if (c === -1) break;
let o = s.substring(0, c).lastIndexOf("(");
let start = s.substring(0, o);
let middle = s.substring(o + 1, c).split("").reverse().join("");
let end = s.substring(c + 1, s.length);
s = start + middle + end;
}
return s;
}
In Python:
Simple Method
def reverseInParentheses(s):
return eval('"' + s.replace('(', '"+("').replace(')', '")[::-1]+"') + '"')
Using Stacks Method
def reverseInParentheses(s):
stack = []
for x in s:
if x == ")":
tmp = ""
while stack[-1] != "(":
tmp += stack.pop()
stack.pop() # pop the (
for item in tmp:
stack.append(item)
else:
stack.append(x)
return "".join(stack)
In C++
Simple Method:-
reverseString
function will reverse the String using the swapping method while reverseParentheses
function will update string recursively.
string reverseString(string s){
for(int i = 0;i < s.length()/2;i++){
char t = s[s.length()-1-i];
s[s.length()-1-i] = s[i];
s[i] = t;
}
return s;
}
string reverseInParentheses(string s) {
int beg = 0;
int end = s.length() - 1;
for(int i = 0; i < s.length(); i++){
if(s[i] == '(')
beg = i;
if(s[i] == ')'){
end = i;
string temp = s.substr(beg + 1, end - beg - 1);
return reverseInParentheses(s.substr(0, beg) + reverseString(temp) + s.substr(end + 1));
}
}
return s;
}
def reverseParentheses(s):
for i in range(len(s)):
if s[i] == "(":
start = i
print (s[:start])
if s[i] == ")":
end = i
print (end)
return reverseParentheses(s[:start] + s[start+1:end][::-1] + s[end+1:])
return s
Here is a solution:
const reverse = (str) => str.split('').reverse().join('');
const reverseParentheses = (s) => {
while (s.includes('(')) {
var l = s.lastIndexOf('(');
var r = s.indexOf(')', s.lastIndexOf('('));
s = s.slice(0, l) + reverse(s.slice(l + 1, r)) + (r + 1 === s.length ? s.slice(r, -1) : s.slice(r + 1));
}
return s;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With