ALGORITHM/BOJ

[백준_javascript] 심화 1 (1157, 2941, 1316, 25206)

오늘도 코딩하나 2024. 8. 11. 17:16

#1157 (단어 공부)

solution1_Object.

const fs = require('fs');
const file = process.platform === 'linux' ? '/dev/stdin' : __dirname+'/input.txt';

const input = fs.readFileSync(file).toString().trim().toLowerCase().split('');
const result = [];
input.forEach(x => {
    result[x] = (result[x] || 0) + 1;
})
const max = Math.max(...Object.values(result));
const maxKey = Object.keys(result).find(x => result[x] === max);
const maxLen = Object.values(result).filter(x => x === max);

console.log(maxLen.length > 1 ? '?' : maxKey.toUpperCase());

 

 

solution2_charCodeAt().

const fs = require('fs');
const file = process.platform === 'linux' ? '/dev/stdin' : __dirname+'/input.txt';

const input = fs.readFileSync(file).toString().trim().toUpperCase().split('');

const arr = new Array(26).fill(0);

for(let i of input) {
    const char = i.charCodeAt();
    arr[char-65] += 1;
}

const max = Math.max(...arr);
const maxCnt = arr.filter(x => x === max).length;

const index = arr.indexOf(max);
const result = String.fromCharCode(index + 65);

console.log(maxCnt === 1 ? result : '?');

 

#2941 (크로아티아 알파벳)

solution1_includes().

const fs = require('fs');
const file = process.platform === 'linux' ? '/dev/stdin' : __dirname+'/input.txt';

let input = fs.readFileSync(file).toString().trim();
const alpha = ['c=', 'c-', 'dz=', 'd-', 'lj', 'nj', 's=', 'z='];

alpha.forEach(x => {
    if(input.includes(x)) {
        input = input.replaceAll(x,'.');
    }
});
console.log(input.length);

 

solution2_replaceAll().

const fs = require('fs');
const file = process.platform === 'linux' ? '/dev/stdin' : __dirname+'/input.txt';

let input = fs.readFileSync(file).toString().trim();
const alpha = ['c=', 'c-', 'dz=', 'd-', 'lj', 'nj', 's=', 'z='];

for(let x of alpha) {
    input = input.replaceAll(x,'.');
}
console.log(input.length);

 

 

### 틀렸습니다.

const fs = require('fs');
const file = process.platform === 'linux' ? '/dev/stdin' : __dirname+'/input.txt';

let input = fs.readFileSync(file).toString().trim();
const alpha = ['c=', 'c-', 'dz=', 'd-', 'lj', 'nj', 's=', 'z='];
let cnt = alpha.filter(x => input.includes(x));

let count = 0;
alpha.forEach(x => {
    if(input.includes(x)) {
        count += input.split(x).length-1;
        input = input.replaceAll(x,'');
    }
});
console.log(count + input.length);

 ▶ 초기 내가 작성한 소스코드다.

      예제대로 테스트했을 때는 정상출력되는 줄 알았으나,

      ' nljj'로 테스트 했을 때, alpha에서 lj만 걸려야하는데, lj가 걸리고 for문 안에서 replace되면서 nj까지 count가 되는 문제        가 발생했다.

      ⇒ 그래서 수정한 후.. solution1의 소스코드를 최종 작성했다.

 

 

### 참고한 블로그

https://hianna.tistory.com/459

 

[Javascript] 배열 중복 값 개수 구하기

배열에 있는 값들이 몇번이나 중복 되는지 찾는 방법을 소개합니다. forEach() 이용하기 reduce() 이용하기 Map 객체 이용하기 1. forEach() 이용하기 const arr = ['a', 'b', 'a', 'b', 'c']; const result = {}; arr.forEach(

hianna.tistory.com

https://inpa.tistory.com/entry/JS-%F0%9F%9A%80-value%EA%B0%92%EC%9C%BC%EB%A1%9C-key%EA%B0%92-%EC%B0%BE%EA%B8%B0

 

🚀 자바스크립트 객체 value 값으로 key 값 찾기

value값으로 key값 찾기 Object 메소드와 배열 메소드를 적절히 조합하여 사용하면 된다. 원리만 알면 쉽게 구현이 가능하다 function getKeyByValue(object, value) { return Object.keys(object).find(key => object[key] === v

inpa.tistory.com

https://parkparkpark.tistory.com/65

 

[백준 2941] 크로아티아 알파벳 자바스크립트(nodejs)

https://www.acmicpc.net/problem/2941 2941번: 크로아티아 알파벳 예전에는 운영체제에서 크로아티아 알파벳을 입력할 수가 없었다. 따라서, 다음과 같이 크로아티아 알파벳을 변경해서 입력했다. 크로아

parkparkpark.tistory.com