Skip to content

Commit ebda708

Browse files
committedJan 22, 2020
Add solution #1249
1 parent e0b250e commit ebda708

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
1189|[Maximum Number of Balloons](./1189-maximum-number-of-balloons.js)|Easy|
4141
1207|[Unique Number of Occurrences](./1207-unique-number-of-occurrences.js)|Easy|
4242
1232|[Check If It Is a Straight Line](./1232-check-if-it-is-a-straight-line.js)|Easy|
43+
1249|[Minimum Remove to Make Valid Parentheses](./1249-minimum-remove-to-make-valid-parentheses.js)|Medium|
4344
1252|[Cells with Odd Values in a Matrix](./1252-cells-with-odd-values-in-a-matrix.js)|Easy|
4445
1287|[Element Appearing More Than 25% In Sorted Array](./1287-element-appearing-more-than-25-in-sorted-array.js)|Easy|
4546
1290|[Convert Binary Number in a Linked List to Integer](./1290-convert-binary-number-in-a-linked-list-to-integer.js)|Easy|
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 1249. Minimum Remove to Make Valid Parentheses
3+
* https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/
4+
* Difficulty: Medium
5+
*
6+
* Given a string s of '(' , ')' and lowercase English characters.
7+
*
8+
* Your task is to remove the minimum number of parentheses ( '(' or ')',
9+
* in any positions ) so that the resulting parentheses string is valid
10+
* and return any valid string.
11+
*
12+
* Formally, a parentheses string is valid if and only if:
13+
* - It is the empty string, contains only lowercase characters, or
14+
* - It can be written as AB (A concatenated with B), where A and B are valid strings, or
15+
* - It can be written as (A), where A is a valid string.
16+
*/
17+
18+
/**
19+
* @param {string} s
20+
* @return {string}
21+
*/
22+
var minRemoveToMakeValid = function(s) {
23+
const inputArray = s.split('');
24+
const stack = [];
25+
26+
inputArray.forEach((letter, index) => {
27+
if (letter === '(') {
28+
stack.push(index);
29+
} else if (letter === ')') {
30+
if (stack.length) {
31+
stack.pop();
32+
} else {
33+
inputArray[index] = '';
34+
}
35+
}
36+
});
37+
38+
stack.forEach(index => inputArray[index] = '');
39+
return inputArray.join('');
40+
};

0 commit comments

Comments
 (0)
Please sign in to comment.