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

[๋ฐฑ์ค€][Node.js] 1919๋ฒˆ : ์• ๋„ˆ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2021. 7. 16.

 

 

Algorithm

-  ์• ๋„ˆ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ

 


 

๋ฌธ์ œ

๋‘ ์˜์–ด ๋‹จ์–ด๊ฐ€ ์ฒ ์ž์˜ ์ˆœ์„œ๋ฅผ ๋’ค๋ฐ”๊พธ์–ด ๊ฐ™์•„์งˆ ์ˆ˜ ์žˆ์„ ๋•Œ, ๊ทธ๋Ÿฌํ•œ ๋‘ ๋‹จ์–ด๋ฅผ ์„œ๋กœ ์• ๋„ˆ๊ทธ๋žจ ๊ด€๊ณ„์— ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด occurs๋ผ๋Š” ์˜์–ด ๋‹จ์–ด์™€ succor๋Š” ์„œ๋กœ ์• ๋„ˆ๊ทธ๋žจ ๊ด€๊ณ„์— ์žˆ๋Š”๋ฐ, occurs์˜ ๊ฐ ๋ฌธ์ž๋“ค์˜ ์ˆœ์„œ๋ฅผ ์ž˜ ๋ฐ”๊พธ๋ฉด succor์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

ํ•œ ํŽธ, dared์™€ bread๋Š” ์„œ๋กœ ์• ๋„ˆ๊ทธ๋žจ ๊ด€๊ณ„์— ์žˆ์ง€ ์•Š๋‹ค. ํ•˜์ง€๋งŒ dared์—์„œ ๋งจ ์•ž์˜ d๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , bread์—์„œ ์ œ์ผ ์•ž์˜ b๋ฅผ ์ œ๊ฑฐํ•˜๋ฉด, ared์™€ read๋ผ๋Š” ์„œ๋กœ ์• ๋„ˆ๊ทธ๋žจ ๊ด€๊ณ„์— ์žˆ๋Š” ๋‹จ์–ด๊ฐ€ ๋‚จ๊ฒŒ ๋œ๋‹ค.

 

๋‘ ๊ฐœ์˜ ์˜์–ด ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋‘ ๋‹จ์–ด๊ฐ€ ์„œ๋กœ ์• ๋„ˆ๊ทธ๋žจ ๊ด€๊ณ„์— ์žˆ๋„๋ก ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ ์ œ๊ฑฐํ•ด์•ผ ํ•˜๋Š” ์ตœ์†Œ ๊ฐœ์ˆ˜์˜ ๋ฌธ์ž ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•  ๋•Œ์—๋Š” ์•„๋ฌด ์œ„์น˜์— ์žˆ๋Š” ๋ฌธ์ž๋“ ์ง€ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

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

 

๋ฌธ์ œ ํ’€๊ธฐ

let fs = require("fs");
let input = fs.readFileSync("/dev/stdin").toString().split("\n");
let alpha = "abcdefghijklmnopqrstuvwxyz".split("");
alpha = new Map(alpha.map((el, idx) => [el, idx]));

let arr1 = Array(26).fill(0);
let arr2 = Array(26).fill(0);

for (let i = 0; i < input[0].length; i++) {
	arr1[alpha.get(input[0][i])]++;
}
for (let i = 0; i < input[1].length; i++) {
	arr2[alpha.get(input[1][i])]++;
}

let count = 0;

for (let i = 0; i < 26; i++) {
	if (arr1[i] !== arr2[i]) count += Math.abs(arr1[i] - arr2[i]);
}

console.log(count);

 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€