Skip to content

Commit 9984751

Browse files
committed
Add solution #1021
1 parent d918fa3 commit 9984751

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,401 LeetCode solutions in JavaScript
1+
# 1,402 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -837,6 +837,7 @@
837837
1018|[Binary Prefix Divisible By 5](./solutions/1018-binary-prefix-divisible-by-5.js)|Easy|
838838
1019|[Next Greater Node In Linked List](./solutions/1019-next-greater-node-in-linked-list.js)|Medium|
839839
1020|[Number of Enclaves](./solutions/1020-number-of-enclaves.js)|Medium|
840+
1021|[Remove Outermost Parentheses](./solutions/1021-remove-outermost-parentheses.js)|Easy|
840841
1022|[Sum of Root To Leaf Binary Numbers](./solutions/1022-sum-of-root-to-leaf-binary-numbers.js)|Easy|
841842
1023|[Camelcase Matching](./solutions/1023-camelcase-matching.js)|Medium|
842843
1024|[Video Stitching](./solutions/1024-video-stitching.js)|Medium|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 1021. Remove Outermost Parentheses
3+
* https://leetcode.com/problems/remove-outermost-parentheses/
4+
* Difficulty: Easy
5+
*
6+
* A valid parentheses string is either empty "", "(" + A + ")", or A + B, where A and B are valid
7+
* parentheses strings, and + represents string concatenation.
8+
* - For example, "", "()", "(())()", and "(()(()))" are all valid parentheses strings.
9+
*
10+
* A valid parentheses string s is primitive if it is nonempty, and there does not exist a way to
11+
* split it into s = A + B, with A and B nonempty valid parentheses strings.
12+
*
13+
* Given a valid parentheses string s, consider its primitive decomposition: s = P1
14+
* + P2 + ... + Pk, where Pi are primitive valid parentheses strings.
15+
*
16+
* Return s after removing the outermost parentheses of every primitive string in the primitive
17+
* decomposition of s.
18+
*/
19+
20+
/**
21+
* @param {string} s
22+
* @return {string}
23+
*/
24+
var removeOuterParentheses = function(s) {
25+
let result = '';
26+
let depth = 0;
27+
28+
for (const char of s) {
29+
if (char === '(') {
30+
if (depth > 0) result += char;
31+
depth++;
32+
} else {
33+
depth--;
34+
if (depth > 0) result += char;
35+
}
36+
}
37+
38+
return result;
39+
};

0 commit comments

Comments
 (0)