-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove-outermost-parentheses.js
More file actions
36 lines (30 loc) · 949 Bytes
/
Copy pathremove-outermost-parentheses.js
File metadata and controls
36 lines (30 loc) · 949 Bytes
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
const removeOuterParentheses = function(word) {
let result = "";
let openPrnts = 0;
for (let x of word) {
if (x === ")" ) openPrnts--;
if (openPrnts > 0) result += x;
if (x === "(") openPrnts++;
}
return result;
};
/**
Example 1:
Input: "(()())(())"
Output: "()()()"
Explanation:
The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
After removing outer parentheses of each part, this is "()()" + "()" = "()()()".
Example 2:
Input: "(()())(())(()(()))"
Output: "()()()()(())"
Explanation:
The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))".
After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".
Example 3:
Input: "()()"
Output: ""
Explanation:
The input string is "()()", with primitive decomposition "()" + "()".
After removing outer parentheses of each part, this is "" + "" = "".
*/