-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-correct-parenthesis.js
More file actions
51 lines (45 loc) · 1.09 KB
/
Copy path1-correct-parenthesis.js
File metadata and controls
51 lines (45 loc) · 1.09 KB
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
/** [올바른 괄호]
* 괄호가 입력되면 올바른 괄호이면 'YES', 올바르지 않으면 'NO'를 출력합니다
* (())() 이것은 괄호의 쌍이올바르게 위치하는 거지만, (()()))은 올바른 괄호가 아닙니다.
*
* 조건: 문자열 최대 길이는 30
* */
// Test Function
function test(str) {
if (str.length > 30) {
console.warn('최대 입력 글자수는 30개입니다');
return;
}
const PARENTHESIS = {
OPEN: '(',
CLOSE: ')',
};
let result = 'YES';
const arr = [...str];
const stack = [];
for (let item of arr) {
if (item === PARENTHESIS.OPEN) {
stack.push(item);
} else if (item === PARENTHESIS.CLOSE) {
if (stack.length) {
stack.pop();
} else {
result = 'NO';
break;
}
}
}
if (stack.length) {
result = 'NO';
}
console.log(`Result is >> ${result} <<`);
return result;
}
// Execute Test
const exampleStrList = [
'(()(()))(()',
'()(()(())',
'(())()',
'()(()(())()(()(())()(()(())()(()(())()(()(())',
];
exampleStrList.forEach(exampleStr => test(exampleStr));