๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ์ธ๊ณต๋ถ€/Algorithm

[๋ฐฑ์ค€][Node.js] 4949๋ฒˆ : ๊ท ํ˜•์žกํžŒ ์„ธ์ƒ

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2021. 10. 28.

 

 

Algorithm

-  ๊ท ํ˜•์žกํžŒ ์„ธ์ƒ -

 


 

๋ฌธ์ œ

์„ธ๊ณ„๋Š” ๊ท ํ˜•์ด ์ž˜ ์žกํ˜€์žˆ์–ด์•ผ ํ•œ๋‹ค. ์–‘๊ณผ ์Œ, ๋น›๊ณผ ์–ด๋‘  ๊ทธ๋ฆฌ๊ณ  ์™ผ์ชฝ ๊ด„ํ˜ธ์™€ ์˜ค๋ฅธ์ชฝ ๊ด„ํ˜ธ์ฒ˜๋Ÿผ ๋ง์ด๋‹ค.

์ •๋ฏผ์ด์˜ ์ž„๋ฌด๋Š” ์–ด๋–ค ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ด„ํ˜ธ๋“ค์˜ ๊ท ํ˜•์ด ์ž˜ ๋งž์ถฐ์ ธ ์žˆ๋Š”์ง€ ํŒ๋‹จํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์งœ๋Š” ๊ฒƒ์ด๋‹ค.

๋ฌธ์ž์—ด์— ํฌํ•จ๋˜๋Š” ๊ด„ํ˜ธ๋Š” ์†Œ๊ด„ํ˜ธ("()") ์™€ ๋Œ€๊ด„ํ˜ธ("[]")๋กœ 2์ข…๋ฅ˜์ด๊ณ , ๋ฌธ์ž์—ด์ด ๊ท ํ˜•์„ ์ด๋ฃจ๋Š” ์กฐ๊ฑด์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • ๋ชจ๋“  ์™ผ์ชฝ ์†Œ๊ด„ํ˜ธ("(")๋Š” ์˜ค๋ฅธ์ชฝ ์†Œ๊ด„ํ˜ธ(")")์™€๋งŒ ์ง์„ ์ด๋ค„์•ผ ํ•œ๋‹ค.
  • ๋ชจ๋“  ์™ผ์ชฝ ๋Œ€๊ด„ํ˜ธ("[")๋Š” ์˜ค๋ฅธ์ชฝ ๋Œ€๊ด„ํ˜ธ("]")์™€๋งŒ ์ง์„ ์ด๋ค„์•ผ ํ•œ๋‹ค.
  • ๋ชจ๋“  ์˜ค๋ฅธ์ชฝ ๊ด„ํ˜ธ๋“ค์€ ์ž์‹ ๊ณผ ์ง์„ ์ด๋ฃฐ ์ˆ˜ ์žˆ๋Š” ์™ผ์ชฝ ๊ด„ํ˜ธ๊ฐ€ ์กด์žฌํ•œ๋‹ค.
  • ๋ชจ๋“  ๊ด„ํ˜ธ๋“ค์˜ ์ง์€ 1:1 ๋งค์นญ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค. ์ฆ‰, ๊ด„ํ˜ธ ํ•˜๋‚˜๊ฐ€ ๋‘˜ ์ด์ƒ์˜ ๊ด„ํ˜ธ์™€ ์ง์ง€์–ด์ง€์ง€ ์•Š๋Š”๋‹ค.
  • ์ง์„ ์ด๋ฃจ๋Š” ๋‘ ๊ด„ํ˜ธ๊ฐ€ ์žˆ์„ ๋•Œ, ๊ทธ ์‚ฌ์ด์— ์žˆ๋Š” ๋ฌธ์ž์—ด๋„ ๊ท ํ˜•์ด ์žกํ˜€์•ผ ํ•œ๋‹ค.

์ •๋ฏผ์ด๋ฅผ ๋„์™€ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ๊ท ํ˜•์žกํžŒ ๋ฌธ์ž์—ด์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ํŒ๋‹จํ•ด๋ณด์ž.

 

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

 

๋ฌธ์ œ ํ’€๊ธฐ

let fs = require("fs");
let input = fs.readFileSync("/dev/stdin")
		.toString()
		.trim()
		.split("\n");
input.pop();
let find = ["(", "[", "]", ")"];
let specialChar = input.map((el) =>
	el.split("").filter((el) => find.includes(el))
);

for (let i = 0; i < specialChar.length; i++) {
	let box = [];
	let torf = true;
	for (let j = 0; j < specialChar[i].length; j++) {
		if (["(", "["].includes(specialChar[i][j])) box.push(specialChar[i][j]);
		else if (specialChar[i][j] === ")") {
			if (box[box.length - 1] === "(") box.pop();
			else {
				torf = false;
				break;
			}
		} else {
			if (box[box.length - 1] === "[") box.pop();
			else {
				torf = false;
				break;
			}
		}
	}
	if (box.length !== 0) console.log("no");
	else if (torf) console.log("yes");
	else console.log("no");
}

 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€