-
Notifications
You must be signed in to change notification settings - Fork 3
/
1021-remove-outermost-parentheses.js
134 lines (109 loc) · 3.97 KB
/
1021-remove-outermost-parentheses.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// 1021. Remove Outermost Parentheses
// https://leetcode.com/problems/remove-outermost-parentheses/
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 60 ms, faster than 72.50% of JavaScript online submissions
// Memory Usage: 36.4 MB, less than 31.58% of JavaScript online submissions
// /**
// * @param {string} s
// * @return {string}
// */
// const removeOuterParentheses = S => {
// let result = '';
// for (let [i, level] = [0, 0]; i < S.length; i++) {
// const char = S[i];
// if ('(' === char) level++;
// if (1 < level) result += char;
// if (')' === char) level--;
// }
// return result;
// };
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 72 ms, faster than 24.86% of JavaScript online submissions
// Memory Usage: 36.3 MB, less than 42.10% of JavaScript online submissions
// /**
// * @param {string} s
// * @return {string}
// */
// const removeOuterParentheses = S => {
// let result = '';
// for (let [i, level] = [0, 0]; i < S.length; i++) {
// if ('(' === S[i]) level++;
// if (1 < level) result += S[i];
// if (')' === S[i]) level--;
// }
// return result;
// };
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 64 ms, faster than 51.99% of JavaScript online submissions
// Memory Usage: 35.5 MB, less than 73.68% of JavaScript online submissions
// /**
// * @param {string} s
// * @return {string}
// */
// const removeOuterParentheses = S => {
// let result = '';
// for (let [t, h, d] = [0, 1, 1]; h < S.length; h++) {
// if (S[h] === '(') d++;
// else if (S[h] === ')') d--;
// if (0 === d) [result, t] = [result + S.slice(t + 1, h), h + 1];
// }
// return result;
// };
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 56 ms, faster than 86.77% of JavaScript online submissions
// Memory Usage: 35.4 MB, less than 73.68% of JavaScript online submissions
// /**
// * @param {string} S
// * @return {string}
// */
// const removeOuterParentheses = S => {
// let result = '';
// for (let [begin, end, depth] = [0, 1, 1]; end < S.length; end++) {
// if (S[end] === '(') depth++;
// else if (S[end] === ')') depth--;
// if (0 === depth) {
// result += S.slice(begin + 1, end);
// begin = end + 1;
// }
// }
// return result;
// };
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 60 ms, faster than 70.75% of JavaScript online submissions
// Memory Usage: 38.5 MB, less than 11.11% of JavaScript online submissions
// /**
// * @param {string} s
// * @param {string} [res='']
// * @param {number} [depth=0]
// */
// const removeOuterParentheses = (s, res = '', depth = 0) =>
// 0 === s.length
// ? res
// : removeOuterParentheses(
// s.slice(1),
// 0 === depth || (1 === depth && ')' === s[0]) ? res : res + s[0],
// '(' === s[0] ? depth + 1 : depth - 1,
// );
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 76 ms, faster than 20.33% of JavaScript online submissions
// Memory Usage: 38.4 MB, less than 11.11% of JavaScript online submissions
/**
* @param {string} s
* @param {string} [res='']
* @param {number} [depth=0]
*/
const removeOuterParentheses = (s, res = '', depth = 0) =>
0 === s.length
? res
: removeOuterParentheses(
s.slice(1),
res + (0 === depth || (1 === depth && ')' === s[0]) ? '' : s[0]),
depth + ('(' === s[0] ? 1 : -1),
);
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
import { strictEqual } from 'assert';
strictEqual(removeOuterParentheses('(()())(())'), '()()()');
strictEqual(removeOuterParentheses('(()())(())(()(()))'), '()()()()(())');
strictEqual(removeOuterParentheses('()()'), '');
strictEqual(removeOuterParentheses('()'), '');
strictEqual(removeOuterParentheses('((((()))))'), '(((())))');