html
html
1
Enter to Rename, Shift+Enter to Preview
css
css
1
Enter to Rename, Shift+Enter to Preview
js
js
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
/*
Simplify a given algebraic string of characters, ‘+’, ‘-‘ operators and parentheses.
Output the simplified string without parentheses.
Examples:
Input : "a-(b+c)" Output : "a-b-c"
Input : "a-(b-c-(d+e))-f" Output : "a-b+c+d+e-f"
*/
let s = 'a-(b-c-(d+e))-f';
// Save regular expression in a variable.
// This regex looks for the most inner paranthesis.
let par = new RegExp('\\([\\w\\d+-]+\\)');
// Remove whitespace
s = s.replace(/\s*/g,'');
// While we still find paranthesis in the string
while (s.match(par)) {
// matched paranthesis.
// first loop with this example it will be: (d+e), then: (b-c-d-e)
let matched = s.match(par);
// position where we found this expression.
// first loop with this example it will be: 7, then: 2
let i = s.indexOf(matched);
Enter to Rename, Shift+Enter to Preview
BROWSER
Console
Run