-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5-iron-bars.js
More file actions
39 lines (34 loc) · 833 Bytes
/
Copy path5-iron-bars.js
File metadata and controls
39 lines (34 loc) · 833 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
37
38
39
function solution(str) {
if (str.length > 100000) {
console.warn('입력 가능한 최대 문자열 길이는 100000입니다.');
return;
}
const PARENTHESIS = {
OPEN: '(',
CLOSE: ')',
};
let piece = 0;
const stack = [];
let prevStr = '';
[...str].forEach((item, idx) => {
if (item === PARENTHESIS.OPEN) {
stack.push(item);
} else {
const str1 = stack.pop();
if (str1 === prevStr) { // 막대기 끝
piece += 1;
} else if (str1 !== prevStr && stack.length) { // 레이저일 경우
piece += stack.length;
}
}
prevStr = item;
})
console.log(piece);
return piece;
}
// Execute Test
const exampleStrList = [
'()(((()())(())()))(())',
'(((()(()()))(())()))(()())',
];
exampleStrList.forEach(exampleStr => solution(exampleStr));