Skip to content

Commit d3cf4e7

Browse files
committed
valid-parentheses solutions
1 parent a35c403 commit d3cf4e7

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

valid-parentheses/Blossssom.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @param s - 괄호 문자열
3+
* @returns - 올바르게 열고 닫혔는지 반환
4+
*/
5+
// function isValid(s: string): boolean {
6+
// const parentheses: Record<string, boolean> = {
7+
// "()": true,
8+
// "{}": true,
9+
// "[]": true,
10+
// };
11+
// const stack: string[] = [];
12+
// for (const st of s) {
13+
// if (
14+
// !stack.length ||
15+
// parentheses[`${stack[stack.length - 1]}${st}`] === undefined
16+
// ) {
17+
// stack.push(st);
18+
// } else {
19+
// stack.pop();
20+
// }
21+
// }
22+
// return stack.length ? false : true;
23+
// }
24+
25+
function isValid(s: string): boolean {
26+
const parentheses: Record<string, string> = {
27+
"(": ")",
28+
"{": "}",
29+
"[": "]",
30+
};
31+
32+
const stack: string[] = [];
33+
for (const char of s) {
34+
if (parentheses[char]) {
35+
stack.push(char);
36+
} else {
37+
const last = stack.pop();
38+
if (last === undefined || parentheses[last] !== char) {
39+
return false;
40+
}
41+
}
42+
}
43+
44+
return !stack.length;
45+
}
46+
47+

0 commit comments

Comments
 (0)