File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments