문자열


11654번 (아스키 코드)

const fs = require("fs");
const input = fs.readFileSync("./input.txt").toString().trim();
console.log(input.charCodeAt(0));
  • charCodeAt() 함수 이용
  • 11720번 (숫자의 합)

    const fs = require("fs");
    const input = fs.readFileSync("./input.txt").toString().trim().split('\n');
    const arr = input[1].toString().split('').map(Number);
    let result = 0;
    for (let i = 0; i < parseInt(input[0]); i++) {
    result += arr[i];
    }
    console.log(result);
  • for문을 이용하여 result라는 변수에 두번째 줄의 숫자들을 모두 더해주었다. i는 첫번째 줄의 숫자를 참조해서 상한을 정했다.
  • 10809번 (알파벳 찾기)

    const fs = require("fs");
    const input = fs.readFileSync("./input.txt").toString().trim().split('');
    let result = [];
    for (let i = 97; i <= 122; i++) {
    result.push(input.indexOf(String.fromCharCode(i)))
    }
    console.log(result.join(' '));
  • a~z는 아스키코드 97~122이다. 아스키코드를 통해 문자 a~z를 가져와서 result라는 빈 배열에 indexOf을 이용하여 인덱스 숫자를 담아준다.
  • join()을 이용해 문제에서 제시한 출력 형식에 맞게 출력한다.
  • 2675번 (문자열 반복)

    const fs = require("fs");
    const input = fs.readFileSync("./input.txt").toString().trim().split('\n');
    let arr = [];
    let arr2 = [];
    let result = '';
    for (let i = 1; i <= parseInt(input[0]); i++) {
    arr = input[i].trim().split(' ');
    for (let j = 0; j < arr[1].length; j++) {
    arr2 = arr[1].trim().split('');
    result += arr2[j].repeat(arr[0]);
    }
    result += '\n'
    }
    console.log(result);
  • 반복해야 할 문자열을 arr2에 담았다.
  • .repeat()함수를 이용하여 반복해주었고 두번째 for문 밖에 ‘\n’를 출력하여 줄바꿈을 해주었다.
  • 1157번 (단어 공부)

    const fs = require("fs");
    const input = fs.readFileSync("./input.txt").toString().trim()
    .toUpperCase().split('').map(x => x.charCodeAt());
    const ascii = new Array(26).fill(0);
    for (let j = 0; j < input.length; j++) {
    for (let i = 0; i < 27; i++) {
    if (input[j] === i+65) {
    ascii[i]++;
    }
    }
    }
    let index = 0;
    let index2 = 0;
    index = ascii.indexOf(Math.max(...ascii));
    index2 = ascii.lastIndexOf(Math.max(...ascii));
    if (index === index2) {
    console.log(String.fromCharCode(index+65));
    } else {
    console.log('?');
    }
  • input을 모두 대문자로 변환한 다음, 배열로 쪼개고 모두 아스키코드로 바꿔주었다.
  • ascii라는 이름의 0으로 채워진 26개의 요소를 가진 배열을 선언하였다.
  • for문을 이용하여 input의 요소마다 i+65(65~92)의 아스키코드를 가진 값과 비교하여 같을 경우 해당 위치의 숫자를 1씩 증가시켜주었다.
  • 그렇게 증가된 ascii 배열을 가지고 최댓값의 인덱스를 찾아주었고 최댓값이 두개 이상인 경우 ?를 출력해야 하기때문에 뒤쪽에서부터의 인덱스 값도 찾아주어 만약 두 인덱스가 다르면 ?를 출력하도록 하였다.
  • 1152번 (단어의 개수)

    const fs = require("fs");
    const input = fs.readFileSync("/dev/stdin").toString().trim().split(' ');
    if (input[0] === "") {
    console.log(0);
    } else {
    console.log(input.length);
    }
  • 빈 값이 들어오는 경우 1이 출력돼서 계속 오답이 됐다.
  • 빈 값이 들어오는 경우를 따로 상정해서 0을 출력해주었다.
  • 2908번 (상수)

    const fs = require("fs");
    const input = fs.readFileSync("./input.txt").toString().trim().split(' ');
    let input1 = input[0].split('');
    let input2 = input[1].split('');
    input1 = input1.reverse().join('');
    input2 = input2.reverse().join('');
    if (input1 > input2) {
    console.log(input1);
    } else {
    console.log(input2);
    }
  • 각 숫자를 배열로 자른 뒤 reverse()를 이용해 뒤집어주고 join()으로 이어줬다.
  • 두 수를 비교하여 큰 수를 출력했다.
  • 5622번 (다이얼)

    const fs = require("fs");
    const input = fs.readFileSync("./input.txt").toString().trim().split('');
    let num = 0;
    const dial = {
    2 : 'ABC',
    3 : 'DEF',
    4 : 'GHI',
    5 : 'JKL',
    6 : 'MNO',
    7 : 'PQRS',
    8 : 'TUV',
    9 : 'WXYZ'
    }
    for (let i = 0; i < input.length; i++) {
    for (let j = 2; j <= 9; j++) {
    if (dial[j].includes(input[i])) {
    num += j + 1;
    }
    }
    }
    console.log(num);
  • dial이라는 객체를 만들어 전화기 다이얼의 정보를 담았다.
  • num이라는 변수를 설정하고 그곳에 결과값을 담았다.
  • for문을 돌려서 input의 인덱스를 i로, 객체의 숫자값을 j로 설정해주었다.
  • dial[j]에 input[i]가 포함된 property의 숫자를 num에 더해준다.
  • 2941번 (크로아티아 알파벳)

    const fs = require("fs");
    const input = fs.readFileSync("./input.txt").toString().trim();
    const croatia = {
    1 : 'c=',
    2 : 'c-',
    3 : 'dz=',
    4 : 'd-',
    5 : 'lj',
    6 : 'nj',
    7 : 's=',
    8 : 'z='
    }
    let answer = input;
    for (let i = 1; i < 9; i++) {
    if (input.includes(croatia[i])) {
    answer = answer.replaceAll(croatia[i], 'A');
    }
    }
    console.log(answer.length);
  • 크로아티아 알파벳을 객체에 담았다. 키값은 for문 호출을 위해 숫자를 1에서 8까지의 숫자로 하였다.
  • answer라는 변수에 input 문자열을 복사해주었다.
  • for문을 돌려 input에 croatia[i]가 포함되어 있을 경우 1자리 알파벳 ‘A’로 바꿔주도록 하였다.
  • answer의 길이를 출력하였다.
  • 1316번 (그룹 단어 체커)

    const fs = require("fs");
    const input = fs.readFileSync("./input.txt").toString().trim().split('\n');
    let answer = 0;
    let lastArr = [];
    for (let i = 1; i < input.length; i++) {
    let arr = input[i].split('');
    for (let j = 0; j < arr.length; j++) {
    if (arr[j] == arr[j+1]) {
    arr.splice(j+1, 1);
    j--;
    }
    }
    lastArr = [...new Set(arr)];
    if (arr.length == lastArr.length) {
    answer++;
    }
    }
    console.log(answer);
  • input의 모양이 \n로 구분되어 있으므로 \n를 기준으로 자르도록 하여 배열에 담았다.
  • 정답을 담을 answer 변수를 선언하고 0으로 초기화하였다.
  • 이중 for문을 실행하였다. 바깥 쪽 for문은 input 배열의 0번째 인덱스를 제외한 다른 인덱스에 존재하는 단어를 배열로 쪼개어 arr라는 새로운 배열에 담아준다.
  • 안쪽 for문은 arr 배열에 연속해서 나타나는 알파벳 중 뒤쪽 알파벳을 제거해준다. 이로써 다시 한 번 중복이 일어날 경우 그룹 단어가 아니게 된다.
  • 안쪽 for문에서 만약 연속되는 알파벳이 나타나 인덱스를 제거한 경우 arr배열의 길이가 1 감소하기 때문에 이 경우 j를 1 감소시켜 주었다.
  • 미리 초기화 해두었던 lastArr라는 배열에 Set()을 이용하여 arr배열의 중복을 제거한 배열을 만들어 담는다.
  • 여기서 다시 한 번 중복이 일어나면 arr와 lastArr의 길이가 달라지게 되고 이는 그룹단어가 아니라는 뜻이다. 두 배열의 길이가 같으면 answer를 1 증가시켜 준다.
  • answer를 출력한다.