--- title: 'Valid Parentheses (Level 2)' author: '์ํ' date: 2024-10-14T13:53:35+09:00 category: ['POSTS'] tags: ['Javascript', 'Algorithm'] og_image: "/images/gamer.png" keywords: ['Javascript', 'Algorithm'] --- This exact problem appeared in a live coding test for the company I want to join. I had solved this problem three years ago while preparing for coding tests as a new graduate, but I couldn't remember it since it's been a while. It was so frustrating that I couldn't solve it halfway through, so I decided to review it. ```js function solution(s) { let stack = []; let obj = { '(': ')', '{':'}', '[':']' }; for (let i = 0; i < s.length; i++) { if (Object.keys(obj).includes(s[i])) { stack.push(s[i]); // Only push the left parentheses onto the stack. } else { let last = stack.pop(); // Pop the last item from the stack. if (s[i] !== obj[last]) { // If it doesn't match the correct closing bracket, return false. return false; } } } if (stack.length !== 0) { // If there's anything left in the stack, it means not all parentheses were closed, so return false. return false; } else { return true; // If the stack is empty, all parentheses were correctly matched, so return true. } } ```
ย
--- title: '์ฌ๋ฐ๋ฅธ ๊ดํธ (LV2)' author: '์ํ' date: 2024-10-14T13:53:35+09:00 category: ['POSTS'] tags: ['Javascript', 'Algorithm'] og_image: "/images/gamer.png" keywords: ['Javascript', 'Algorithm'] --- ๊ฐ๊ณ ์ถ์ ํ์ฌ ๋ผ์ด๋ธ ์ฝ๋ฉํ ์คํธ์์ ์ด ๋ฌธ์ ๊ฐ ๊ทธ๋๋ก ๋์๋ค. 3๋ ์ , ์ ์ ์ค๋น ํ ๋ ์ฝ๋ฉํ ์คํธ ์ค๋น ํ๋ฉด์ ๋ถ๋ช ํ ํ์ด๋ดค๋ ๋ฌธ์ ๋ฐ... ์ค๋ ๋์ด์ ๊ธฐ์ต์ด ๋์ง ์์๋ค. ์ค๊ฐ์ ๋ชปํผ๊ฒ, ๋๋ฌด ์์ฌ์ ๋ค. ๊ทธ๋์ ๋ณต๊ธฐ๋ฅผ ํด๋ณด๋ ์ค์ด๋ค. ```js function solution(s) { let stack = []; let obj = { '(': ')', '{':'}', '[':']' }; for (let i = 0; i < s.length; i++) { if (Object.keys(obj).includes(s[i])) { stack.push(s[i]); // ์ข์ธก์ ๊ดํธ๋ง stack์ ๋ฃ๋๋ค. } else { let last = stack.pop(); // stack ๋ฐฐ์ด์ ๊ฐ์ฅ ๋ง์ง๋ง์ ๋นผ์ if (s[i] !== obj[last]) { // ์ฌ๋ฐ๋ฅธ ๊ดํธ์ ๋ง์ถฐ๋ณด๊ณ , ์๋๋ฉด false return false; } } } if (stack.length !== 0) { // stack์ ์ด๋ค ๊ฐ์ด ๋จ์ ์๋ค๋ฉด, ๊ดํธ๊ฐ ๋ชจ๋ ๋ซํ์ง ์์๋ค๋ ์๋ฏธ๊ฐ ๋๋ค. ๊ณ ๋ก, false return false; } else { return true; // stack์ด ๋ชจ๋ ์ ๊ฑฐ๊ฐ ๋์๋ค๋ ๋ป์, ๊ดํธ๊ฐ ๋ชจ๋ ์ฌ๋ฐ๋ฅด๊ฒ ๋ซํ๋ค๋ ์๋ฏธ, ๊ณ ๋ก true } } ```