Skip to content

Commit 07e5641

Browse files
authored
Create 1963-minimum-number-of-swaps-to-make-the-string-balanced.js
1 parent e76ec4d commit 07e5641

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const minSwaps = function(s) {
6+
const stack = []
7+
let num = 0
8+
for(let e of s) {
9+
if(e === '[') {
10+
stack.push(e)
11+
num++
12+
}
13+
if(e === ']') {
14+
if(stack[stack.length - 1] === '[') {
15+
stack.pop()
16+
num--
17+
}
18+
}
19+
}
20+
// console.log(num)
21+
return Math.ceil(num / 2)
22+
};

0 commit comments

Comments
 (0)