-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreverseInParentheses.js
More file actions
78 lines (61 loc) · 2.45 KB
/
Copy pathreverseInParentheses.js
File metadata and controls
78 lines (61 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
function solution(inputString) {
let openingParenthesesIndex = -1
let closingParenthesesIndex = -1
findTheIndexOfTheExpressionToReverse()
while (openingParenthesesIndex != -1) {
reverseExpression()
findTheIndexOfTheExpressionToReverse()
}
function findTheIndexOfTheExpressionToReverse() {
for (let index = 0; index < inputString.length; index++) {
const element = inputString[index]
if (element == '(') {
openingParenthesesIndex = index
} else if (element == ')') {
closingParenthesesIndex = index
break
}
}
}
function reverseExpression() {
// prettier-ignore
let expressionInParentheses = inputString.substring(openingParenthesesIndex,closingParenthesesIndex+1)
let arrayExpressionInParentheses = expressionInParentheses.split('')
let arrayreverseExpression = arrayExpressionInParentheses.reverse()
arrayreverseExpression.pop()
arrayreverseExpression.shift()
let reverseExpression = arrayreverseExpression.toString()
reverseExpression = reverseExpression.replaceAll(',', '')
// prettier-ignore
inputString = inputString.replace(expressionInParentheses,reverseExpression)
openingParenthesesIndex = -1
closingParenthesesIndex = -1
}
console.log(inputString)
return inputString
}
const inputString = 'foo(bar)baz(blim)'
const inputString2 = '(bar)'
solution(inputString2)
// Codewriting
// 300
// Write a function that reverses characters in (possibly nested) parentheses in the input string.
// Input strings will always be well-formed with matching ()s.
// Example
// For inputString = "(bar)", the output should be
// solution(inputString) = "rab";
// For inputString = "foo(bar)baz", the output should be
// solution(inputString) = "foorabbaz";
// For inputString = "foo(bar)baz(blim)", the output should be
// solution(inputString) = "foorabbazmilb";
// For inputString = "foo(bar(baz))blim", the output should be
// solution(inputString) = "foobazrabblim".
// Because "foo(bar(baz))blim" becomes "foo(barzab)blim" and then "foobazrabblim".
// Input/Output
// [execution time limit] 4 seconds (js)
// [input] string inputString
// A string consisting of lowercase English letters and the characters ( and ). It is guaranteed that all parentheses in inputString form a regular bracket sequence.
// Guaranteed constraints:
// 0 ≤ inputString.length ≤ 50.
// [output] string
// Return inputString, with all the characters that were in parentheses reversed.