-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* @param {string} s | ||
* @return {boolean} | ||
*/ | ||
var isValid = function (s) { | ||
// 괄호 관리 스택 | ||
const stack = []; | ||
|
||
// 여는 괄호, 닫는 괄호 매핑 | ||
const brackets = { "(": ")", "{": "}", "[": "]" }; | ||
|
||
// for문 돌며 확인 | ||
for (let i of s) { | ||
// 여는 괄호일 경우 | ||
if (brackets[i]) { | ||
stack.push(brackets[i]); | ||
// 닫는 괄호일 경우 | ||
} else if (i !== stack.pop()) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
|
||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(n) |