본문 바로가기
코테/코테withJS

백준 Node.js(자바스크립트) 2941번 크로아티아 알파벳 문제

by 트레일헤드레인저 2021. 8. 5.
SMALL

문제

풀이

 

const fs = require("fs");
const input = (
  process.platform === "linux"
    ? fs.readFileSync("/dev/stdin").toString()
    : `ljes=njak`
).trim();

let croatia = ["c=", "c-", "dz=", "d-", "lj", "nj", "s=", "z="];

function solution(input) {
  for (let alphabet of croatia) {
    input = input.split(alphabet).join("Q");
  }

  return input.length; // return input일 경우 QeQQak를 반환한다.
}

console.log(solution(input));
const fs = require("fs");
const input = (
  process.platform === "linux"
    ? fs.readFileSync("/dev/stdin").toString()
    : `c=c=`
)
  .trim()
  .split("");

let croatiaWordCount = 0;

for (let i = 0; i < input.length; i++) {
  let liftWord = input[i - 1];
  let nowWord = input[i];
  let rightWord = input[i + 1];
  let rrWord = input[i + 2];

  if (nowWord === "c") {
    if (rightWord === "=" || rightWord === "-") {
      croatiaWordCount++;
      i++;
      continue;
    }
  }

  if (nowWord === "d") {
    if (rightWord === "-") {
      croatiaWordCount++;
      i++;
      continue;
    }
    if (rightWord === "z" && rrWord === "=") {
      croatiaWordCount++;
      i += 2;
      continue;
    }
  }

  if (nowWord === "l" || nowWord === "n") {
    if (rightWord === "j") {
      croatiaWordCount++;
      i++;
      continue;
    }
  }

  if (nowWord === "s") {
    if (rightWord === "=") {
      croatiaWordCount++;
      i++;
      continue;
    }
  }

  if (nowWord === "z") {
    if (liftWord !== "d" && rightWord === "=") {
      croatiaWordCount++;
      i++;
      continue;
    }
  }

  croatiaWordCount++;
}

console.log(croatiaWordCount);
LIST