Skip to content

Commit 8b03d6a

Browse files
committed
Valid Parentheses Solution
1 parent aaeb0b1 commit 8b03d6a

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

โ€Žvalid-parentheses/clara-shin.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* ๊ด„ํ˜ธ ๋ฌธ์ž์—ด ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ
3+
*
4+
* ์Šคํƒ ์ž๋ฃŒ๊ตฌ์กฐ ํ™œ์šฉ
5+
* 1. ๊ด„ํ˜ธ ์Œ์„ ๋งคํ•‘ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์กฐ๊ฑด์„ ํ™•์ธ
6+
* 2. ์—ด๋ฆฐ ๊ด„ํ˜ธ๋ฅผ ๋งŒ๋‚˜๋ฉด ํ•ด๋‹นํ•˜๋Š” ๋‹ซํžŒ ๊ด„ํ˜ธ๋ฅผ ์Šคํƒ์— ์ง์ ‘ push
7+
* 3. ๋‹ซ๋Š” ๊ด„ํ˜ธ๋ฅผ ๋งŒ๋‚ฌ์„ ๋•Œ, ์Šคํƒ์ด ๋น„์–ด์žˆ๊ฑฐ๋‚˜ ์ง์ด ๋งž์ง€ ์•Š์œผ๋ฉด false
8+
* 4. ๋ฌธ์ž์—ด์„ ๋ชจ๋‘ ์ฒ˜๋ฆฌํ•œ ํ›„, ์Šคํƒ์ด ๋น„์–ด์žˆ์–ด์•ผ(๋ฌธ์ž์—ด ๊ธธ์ด๊ฐ€ 0์ด์–ด์•ผ) ๋ชจ๋“  ๊ด„ํ˜ธ๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ง์ง€์–ด์ง„ ๊ฒƒ(true)
9+
*/
10+
11+
/**
12+
* @param {string} s
13+
* @return {boolean}
14+
*/
15+
var isValid = function (s) {
16+
// ๋นˆ ๋ฌธ์ž์—ด์ด๋‚˜ ํ™€์ˆ˜ ๊ธธ์ด๋Š” ์œ ํšจํ•˜์ง€ ์•Š์Œ
17+
if (s.length === 0 || s.length % 2 !== 0) return false;
18+
19+
const stack = [];
20+
21+
for (let i = 0; i < s.length; i++) {
22+
const char = s[i];
23+
24+
if (char === '(') {
25+
stack.push(')');
26+
} else if (char === '{') {
27+
stack.push('}');
28+
} else if (char === '[') {
29+
stack.push(']');
30+
} else if (stack.length === 0 || stack.pop() !== char) {
31+
// ๋‹ซ๋Š” ๊ด„ํ˜ธ๋ฅผ ๋งŒ๋‚ฌ์„ ๋•Œ, ์Šคํƒ์ด ๋น„์–ด์žˆ๊ฑฐ๋‚˜ ์ง์ด ๋งž์ง€ ์•Š์Œ
32+
return false;
33+
}
34+
}
35+
36+
return stack.length === 0;
37+
};

0 commit comments

Comments
ย (0)