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

[๋ฐฑ์ค€][Node.js] 1181๋ฒˆ : ๋‹จ์–ด ์ •๋ ฌ

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

 

 

 

Algorithm

-  ๋‹จ์–ด ์ •๋ ฌ -

 


 

๋ฌธ์ œ

์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ N๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์กฐ๊ฑด์— ๋”ฐ๋ผ ์ •๋ ฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

1. ๊ธธ์ด๊ฐ€ ์งง์€ ๊ฒƒ๋ถ€ํ„ฐ

2. ๊ธธ์ด๊ฐ€ ๊ฐ™์œผ๋ฉด ์‚ฌ์ „ ์ˆœ์œผ๋กœ

 

๊ฐ™์€ ๋‹จ์–ด๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ์—๋Š” ํ•œ ๋ฒˆ์”ฉ๋งŒ ์ถœ๋ ฅํ•œ๋‹ค.

 

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

 

๋ฌธ์ œ ํ’€๊ธฐ

let fs = require("fs");
let input = fs.readFileSync("/dev/stdin")
		.toString()
		.trim()
		.split("\n");
input.shift();

let obj = {};

for (let i = 0; i < input.length; i++) {
	let len = input[i].length;
	if (obj[len]) {
		if (obj[len].includes(input[i])) continue;
		obj[len].push(input[i]);
	} else obj[len] = [input[i]];
}

for (let key in obj) {
	if (obj[key].length === 1) {
		console.log(obj[key][0]);
	} else {
		obj[key].sort();
		console.log(obj[key].join("\n"));
	}
}

 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€