๐Ÿงฎ

Valid Parentheses

Valid Parentheses
๋ณดํ†ต ์•Œ๊ณ ๋ฆฌ์ฆ˜ +20pts

Problem

Write a function that checks if a string of parentheses is valid.

Examples

Input: isValid("(())")
Output: true
Input: isValid("(()")
Output: false

Explanation

์ด ๋ฌธ์ œ๋Š” **์Šคํƒ(Stack)** ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ™œ์šฉํ•œ ๊ด„ํ˜ธ ๊ฒ€์ฆ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋”ฉ ์ธํ„ฐ๋ทฐ์˜ ํด๋ž˜์‹ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. **์Šคํƒ์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ** ๊ด„ํ˜ธ๋Š” LIFO(Last In, First Out) ํŠน์„ฑ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์ตœ๊ทผ์— ์—ด๋ฆฐ ๊ด„ํ˜ธ๊ฐ€ ๋จผ์ € ๋‹ซํ˜€์•ผ ํ•ฉ๋‹ˆ๋‹ค. **์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ „๋žต** 1. ์—ฌ๋Š” ๊ด„ํ˜ธ โ†’ ๋Œ€์‘ํ•˜๋Š” ๋‹ซ๋Š” ๊ด„ํ˜ธ๋ฅผ ์Šคํƒ์— push 2. ๋‹ซ๋Š” ๊ด„ํ˜ธ โ†’ ์Šคํƒ์—์„œ popํ•˜์—ฌ ์ผ์น˜ ํ™•์ธ 3. ๋๊นŒ์ง€ ์ˆœํšŒ ํ›„ ์Šคํƒ์ด ๋น„์–ด์žˆ์œผ๋ฉด ์œ ํšจ **pairs ๊ฐ์ฒด์˜ ์—ญํ• ** `{ '(': ')', '[': ']', '{': '}' }`...

View detailed explanation โ†’

Key Concepts

์Šคํƒ ์ž๋ฃŒ๊ตฌ์กฐ LIFO ์›๋ฆฌ ๊ด„ํ˜ธ ๋งค์นญ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ
Time: O(n) Space: O(n)
solution.js
Ctrl + Enter
Run tests to see results here.