๐งฎ
Valid Parentheses
Valid Parentheses๋ณดํต ์๊ณ ๋ฆฌ์ฆ +20pts
Problem
Write a function that checks if a string of parentheses is valid.
Examples
Input:
isValid("(())")Output:
trueInput:
isValid("(()")Output:
falseExplanation
์ด ๋ฌธ์ ๋ **์คํ(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.