๋ฌธ์ œ

์ „์ฒด ์ง€๋„๋Š” ์ง€๋„1๊ณผ ์ง€๋„2๋ฅผ ๊ฒน์ณ์„œ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. 

์ง€๋„1 ๋˜๋Š” ์ง€๋„2 ์ค‘ ์–ด๋Š ํ•˜๋‚˜๋ผ๋„ ๋ฒฝ์ธ ๋ถ€๋ถ„์€ ์ „์ฒด ์ง€๋„์—์„œ ๋ฒฝ์ด๋‹ค. 

์ง€๋„1 ๊ณผ ์ง€๋„2 ์—์„œ ๋ชจ๋‘ ๊ณต๋ฐฑ์ธ ๋ถ€๋ถ„์€ ์ „์ฒด ์ง€๋„์—์„œ ๊ณต๋ฐฑ์ด๋‹ค. 

 

์ง€๋„1(arr1)๊ณผ ์ง€๋„2(arr2)๋Š” ์ •์ˆ˜ ๋ฐฐ์—ด๋กœ ์•”ํ˜ธํ™”

์•”ํ˜ธํ™”๋œ ๋ฐฐ์—ด์€ ๋ฒฝ ๋ถ€๋ถ„์„ 1, ๊ณต๋ฐฑ ๋ถ€๋ถ„์„ 0์œผ๋กœ ๋ถ€ํ˜ธํ™”ํ–ˆ์„ ๋•Œ ์–ป์–ด์ง€๋Š” ์ด์ง„์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์˜ ๋ฐฐ์—ด์ด๋‹ค. 

 

์กฐ๊ฑด

1. ์ง€๋„1 ๋˜๋Š” ์ง€๋„2 ์ค‘ ์–ด๋Š ํ•˜๋‚˜๋ผ๋„ ๋ฒฝ์ธ ๋ถ€๋ถ„์€ ์ „์ฒด ์ง€๋„์—์„œ ๋ฒฝ์ด๋‹ค.

2. ์ง€๋„1 ๊ณผ ์ง€๋„2 ์—์„œ ๋ชจ๋‘ ๊ณต๋ฐฑ์ธ ๋ถ€๋ถ„์€ ์ „์ฒด ์ง€๋„์—์„œ ๊ณต๋ฐฑ์ด๋‹ค. 

3) ๊ฒน์ณ์ง„ ์ง€๋„๋ฅผ #๊ณผ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด ๋ฐฐ์—ด๋กœ ์ถœ๋ ฅํ•œ๋‹ค. 

 

๋น„ํŠธ์—ฐ์‚ฐ

์ถœ์ œ ์˜๋„๋Š” ๋น„ํŠธ ์—ฐ์‚ฐ์„ ์ด์šฉํ•ด์„œ ํ’€๊ธฐ๋ฅผ ์›ํ–ˆ๋˜ ๋ฌธ์ œ์˜€๋‹ค. 

์ฃผ์–ด์ง„ ์ˆ˜๋ฅผ 2์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ -> ๋น„๊ต -> ๋น„๊ต๊ฒฐ๊ณผ๊ฐ’์„ 10์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ ๋ฐ ์ถœ๋ ฅ

 

๋น„ํŠธ : ์ปดํ“จํ„ฐ์—์„œ ์ •๋ณด ํ‘œํ˜„์˜ ์ตœ์†Œ ๋‹จ์œ„

๋น„ํŠธ์—ฐ์‚ฐ์ž๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ 2์ง„์ˆ˜ ํ‘œํ˜„๋ฒ•

 

๋น„ํŠธ ์—ฐ์‚ฐ์ž

  • & : ๋น„๊ตํ•˜๋Š” ๋น„ํŠธ๊ฐ€ ๋ชจ๋‘ 1์ด๋ฉด 1 ๋ฐ˜ํ™˜
  • | : ๋น„๊ตํ•˜๋Š” ๋น„ํŠธ ์ค‘์—์„œ ํ•˜๋‚˜๋ผ๋„ 1์ด๋ฉด 1๋ฐ˜ํ™˜
  • ^ : ๋น„๊ตํ•˜๋Š” ๋น„ํŠธ๊ฐ€ ๊ฐ™์œผ๋ฉด 0, ๋‹ค๋ฅด๋ฉด 1 ๋ฐ˜ํ™˜
  • << : ์ง€์ •ํ•œ ์ˆ˜ ๋งŒํผ ๋น„ํŠธ ์ „์ฒด๋ฅผ ์™ผ์ชฝ์œผ๋กœ ์ด๋™
  • >> : ์ง€์ •ํ•œ ์ˆ˜ ๋งŒํผ ๋น„ํŠธ ์ „์ฒด๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™
  • >>> : ์ง€์ •ํ•œ ์ˆ˜ ๋งŒํผ ๋น„ํŠธ๋ฅผ ์ „๋ถ€ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™, ์ƒˆ๋กœ์šด ๋น„ํŠธ๋Š” ์ „๋ถ€ 0์ด๋œ๋‹ค. 

 

 

ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

์ผ๋‹จ ์ฒซ ๋ฒˆ์งธ๋กœ๋Š” ์ง€๋„ 2๊ฐœ๋ฅผ ํ•ด์„ํ•ด์„œ ๊ฒน์ณ์•ผ๊ฒ ๋‹ค. 

1) ์‹ญ์ง„์ˆ˜๋กœ ์ฃผ์–ด์ง„ ์ˆซ์ž๋ฅผ ์ด์ง„์ˆ˜๋กœ ๋ฐ”๊พผ๋‹ค. 

2) ์ง€๋„ 2๊ฐœ๋ฅผ(arr1, arr2)๋ฅผ ๊ฒน์นœ๋‹ค(ํ•ฉ์นœ๋‹ค)

 

 

์ฝ”๋“œ

const solution = (n, arr1, arr2) => {
  let answer = [];

  // 2์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , 0์„ ์ฑ„์›Œ์„œ n์ž๋ฆฌ ๋งž์ถ”๊ธฐ
  let binaryNumArr1 = arr1.map((i, idx) => arr1[idx].toString(2).padStart(n, '0'));
  let binaryNumArr2 = arr2.map((i, idx) => arr2[idx].toString(2).padStart(n, '0'));

  // ์ด์ค‘for๋ฌธ์œผ๋กœ ๋น„๊ตํ•˜๋ฉด์„œ #๋˜๋Š” ๊ณต๋ฐฑ์œผ๋กœ ๋งž์ถฐ์ฃผ๊ธฐ
  for (let i = 0; i < n; i++) {
    let allArr = '';

    for (let j = 0; j < n; j++) {
      if (binaryNumArr1[i][j] === '1' || binaryNumArr2[i][j] === '1') {
        allArr += '#';
      } else {
        allArr += ' ';
      }
    }
    answer.push(allArr);
  }
  return answer;
}

toString๋ฉ”์†Œ๋“œ

์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

 

 

๋น„ํŠธ์—ฐ์‚ฐ์„ ์ด์šฉํ•œ ๋‹ค๋ฅธ ํ’€์ด

ํ•œ์ค„๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜๊ฐ€ ์žˆ์—ˆ๋‹ค....

const solution = (n, arr1, arr2) => {
  return arr1.map((i, index) => ('0'.repeat(n) + (i | arr2[index]).toString(2)).slice(-n)).map(i => i.replace(/0/g, ' ').replace(/1/g, '#'));
}

๋ฌธ์ œ

๋™์ ์œผ๋กœ ๊ฒŒ์ž„์‹œ๊ฐ„์„ ๋Š˜๋ ค์„œ ๋‚œ์ด๋„๋ฅผ ์กฐ์ ˆํ•œ๋‹ค. 

์‹คํŒจ์œจ = ์Šคํ…Œ์ด์ง€์— ๋„๋‹ฌํ–ˆ์œผ๋‚˜ ์•„์ง ํด๋ฆฌ์–ดํ•˜์ง€๋ชปํ•œ ํ”Œ๋ ˆ์ด์–ด ์ˆ˜ / ์Šคํ…Œ์ด์ง€์— ๋„๋‹ฌํ•œ ํ”Œ๋ ˆ์ด์–ด ์ˆ˜

 

์ „์ฒด ์Šคํ…Œ์ด์ง€ ๊ฐœ์ˆ˜ N

๊ฒŒ์ž„์„ ์ด์šฉํ•˜๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ํ˜„์žฌ ๋ฉˆ์ถฐ์žˆ๋Š” ์Šคํ…Œ์ด์ง€์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด stages

์‹คํŒจ์œจ์ด ๋†’์€ ์Šคํ…Œ์ด์ง€๋ถ€ํ„ฐ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์Šคํ…Œ์ด์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ด๊ฒจ์žˆ๋Š” ๋ฐฐ์—ด์„ return

 

 

๋ฐฉ๋ฒ•

์‹คํŒจ์œจ์„ ๊ตฌํ•ด์„œ ๋ฐฐ์—ด์•ˆ์— ๋„ฃ๊ณ , ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์„œ return ํ•˜์ž

 

 

์ฝ”๋“œ

const solution = (N, stages) => {
  let answer = [];

  // ์ธ์›์ˆ˜
  let totalNum = stages.length;

  for (let i = 1; i <= N; i++) {
    // ํ˜„์žฌ ์Šคํ…Œ์ด์ง€์— ๋„๋‹ฌํ•œ ์ธ์› ์ฐพ๊ธฐ
    let stageNum = stages.filter(ele => ele === i).length;

    // ์‹คํŒจ์œจ ๊ธฐ๋ณธ ์„ธํŒ…
    let failRatio = 0;

    // ์‹คํŒจ์œจ ๊ณ„์‚ฐ
    if (stageNum === 0) {
      failRatio = 0;
    } else {
      failRatio = (stageNum) / totalNum;
    }

    // ์ธ์›์ˆ˜์—์„œ ํ˜„์žฌ ์Šคํ…Œ์ด์ง€์— ์žˆ๋Š” ์ธ์› ๋นผ์ฃผ๊ธฐ
    totalNum -= stageNum;
    answer.push({ idx: i, ratio: failRatio });
  }

  // ์ •๋ ฌ
  answer.sort((a, b) => {
    // ์‹คํŒจ์œจ์ด ๊ฐ™๋‹ค๋ฉด ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
    if (a.ratio === b.ratio) {
      return a.idx - b.idx;
    } else {
      return b.ratio - a.ratio;
    }
  })
  return answer.map(ele => ele.idx);
}

๋ฌธ์ œ 

์ˆ˜ํฌ์ž๋Š” ์ˆ˜ํ•™์„ ํฌ๊ธฐํ•œ ์‚ฌ๋žŒ์˜ ์ค€๋ง์ž…๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž ์‚ผ์ธ๋ฐฉ์€ ๋ชจ์˜๊ณ ์‚ฌ์— ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ์ „๋ถ€ ์ฐ์œผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž๋Š” 1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฐ์Šต๋‹ˆ๋‹ค. 1๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ... 2๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ... 3๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ... 1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€์˜ ์ •๋‹ต์ด ์ˆœ์„œ๋Œ€๋กœ ๋“ค์€ ๋ฐฐ์—ด answers๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ€์žฅ ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ๋งžํžŒ ์‚ฌ๋žŒ์ด ๋ˆ„๊ตฌ์ธ์ง€ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

 

์กฐ๊ฑด

์‹œํ—˜์€ ์ตœ๋Œ€ 10,000 ๋ฌธ์ œ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.
๋ฌธ์ œ์˜ ์ •๋‹ต์€ 1, 2, 3, 4, 5์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.
๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๋ฅผ ๋ฐ›์€ ์‚ฌ๋žŒ์ด ์—ฌ๋Ÿฟ์ผ ๊ฒฝ์šฐ, returnํ•˜๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”.

 

 

์ฝ”๋“œ

const solution = (answers) => {
  let answer = [];
  const list = [
    [1, 2, 3, 4, 5],
    [2, 1, 2, 3, 2, 4, 2, 5],
    [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
  ];

  let point = [0, 0, 0];
  let max = 0;

  for (let i = 0; i < answers.length; i++) {
    if (answers[i] === list[0][i % 5]) {
      point[0]++;
    }
    if (answers[i] === list[1][i % 8]) {
      point[1]++;
    }
    if (answers[i] === list[2][i % 10]) {
      point[2]++;
    }
  }

  for (let j = 0; j < point.length; j++) {
    if (point[j] > max) {
      max = point[j];
    }
  }

  for (let k = 0; k < point.length; k++) {
    if (max === point[k]) {
      answer.push(k + 1);
    }
  }

  if (answer.length !== 1) {
    answer.sort((a, b) => a - b);
  }
  return answer;
}

์ฐ๋Š” ๋ฐฉ์‹์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐฐ์—ด ์•ˆ์— ๋‹ด๋Š”๋‹ค. for๋ฌธ์œผ๋กœ answers์˜ i์™€ list[0][0] -  list[0][5]๊นŒ์ง€ ์ฐพ๋Š”๋‹ค.  

 

 

๋‹ค๋ฅธ ํ’€์ด

function solution(answers) {
    var answer = [];
    var a1 = [1, 2, 3, 4, 5];
    var a2 = [2, 1, 2, 3, 2, 4, 2, 5]
    var a3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];

    var a1c = answers.filter((a,i)=> a === a1[i%a1.length]).length;
    var a2c = answers.filter((a,i)=> a === a2[i%a2.length]).length;
    var a3c = answers.filter((a,i)=> a === a3[i%a3.length]).length;
    var max = Math.max(a1c,a2c,a3c);

    if (a1c === max) {answer.push(1)};
    if (a2c === max) {answer.push(2)};
    if (a3c === max) {answer.push(3)};


    return answer;
}

์ฐ๋Š” ๋ฐฉ์‹ 1,2,3์„ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด๋†“๋Š”๋‹ค. answers ๋ฐฐ์—ด๊ณผ a1, a2, a3 ๋ฐฐ์—ด์„ filter๋ฉ”์†Œ๋“œ๋กœ ๋‹ค๋ฅธ ๊ฒƒ์€ ๊ฑท์–ด๋‚ด๋ฉฐ ๊ฐ‡์€ ๊ฒƒ ๋งŒ ์žˆ๋Š” ๋ฐฐ์—ด์„ ๊ตฌํ•˜๊ณ  length๋ฅผ ๊ตฌํ•œ๋‹ค. ์„ธ ๊ฐœ ์ค‘์—์„œ ํฐ ๊ฐ’์„ ์ฐพ๋Š”๋‹ค. max์ธ ๊ฐ’์„ answer ๋ฐฐ์—ด์— push ํ•œ๋‹ค. 

๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋ฌธ์ž์—ด ํƒ์ƒ‰, ๋น„๊ตํ•ด์„œ ๋ฌธ์ž์—ด๋กœ ๋ฆฌํ„ดํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.

 

1. ๋ฌธ์ œ

์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ๊ฐ™์€ ๋ฌธ์ž๊ฐ€ ์—ฐ์†์œผ๋กœ ๋ฐ˜๋ณต๋˜๋Š” ๊ฒฝ์šฐ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž ๋ฐ”๋กœ ์˜ค๋ฅธ์ชฝ์— ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ํ‘œ๊ธฐํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฌธ์ž์—ด์„ ์••์ถ•ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋‹จ ๋ฐ˜๋ณตํšŸ์ˆ˜๊ฐ€ 1์ธ ๊ฒฝ์šฐ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค.

 

์ž…๋ ฅ์˜ˆ์ œ | KKHSSSSSSSE

์ถœ๋ ฅ์˜ˆ์ œ | K2HS7E

 

2. ์ ‘๊ทผ๋ฐฉ๋ฒ•

๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋ฌธ์ž์—ด ํƒ์ƒ‰ํ•ด์„œ ๋‘๊ฐœ์”ฉ ๋น„๊ตํ•ด์„œ countingํ•ด์ฃผ๊ณ  ์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ์–ด์„œ ๋„ฃ์–ด์ค€๋‹ค. 

 

 

3. ๋ฌธ์ œ ํ’€์ด 

1. ๊ฒฐ๊ณผ๋ฌผ์ด ๋ฌธ์ž์—ด์ด๋‹ค.

2. ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŒ…ํ•ด์„œ answer๋กœ ์ถœ๋ ฅํ•ด์•ผํ•œ๋‹ค.

3. -> ๋ฌธ์ž์—ด ๋‚ด ๋ฌธ์ž ๋‘๊ฐœ ๋น„๊ตํ•ด์„œ ๊ฐ™์œผ๋ฉด ์นด์šดํŒ… 

    -> ๋‹ค๋ฅธ ๊ฒฝ์šฐ answer์— ๋ฌธ์ž ๋ˆ„์ ํ•˜๊ณ 

    -> ๊ฐœ์ˆ˜๋„ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊ฟ”์„œ answer์— ๋ˆ„์ ํ•œ๋‹ค. (count > 1 ์ธ ๊ฒฝ์šฐ์—๋งŒ)

    -> ์นด์šดํŠธ๋Š” ์›๋ž˜๋Œ€๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ค€๋‹ค. 

function solution(str){
	let answer = "";
    let count = 1;
    for(let i=0; i<str.length; i++){
    	if(str[i] === str[i+1]{
        	count++;
        } else {
        	answer += str[i];
            answer += count.toString();
            count = 1;
        }
    }
    return answer;
}

let str = "KKHSSSSSSSE";
console.log(solution(str));
    

๋ฐ˜๋ณตํšŸ์ˆ˜๊ฐ€ 1์ธ ๊ฒฝ์šฐ ์ƒ๋žตํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์ง€ ๋ชปํ–ˆ๋‹ค. 

 

 

ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

else์•ˆ์— ๋˜ if๋ฌธ์„ ์ž‘์„ฑํ•  ์ˆ˜๊ฐ€ ์žˆ์—ˆ๋‹ค!
function solution(str){
	let answer = ""; //์ถœ๋ ฅ์ด ๋ฌธ์ž์—ด๋กœ ๋‚˜์™€์•ผํ•˜๋‹ˆ๊นŒ
    let count = 1; //์ œ์ผ ์•ž์— ๋ฌธ์žํ•˜๋‚˜๋Š” ๊ธฐ๋ณธ์œผ๋กœ ์žˆ์œผ๋‹ˆ๊นŒ 1๋กœ ์ €์žฅํ•ด์ฃผ๊ธฐ
    for(let i=0; i<str.length: i++){
    	if(str[i] === str[i+1]){
        	count++;
        } else {
        	answer += str[i]; // answer์— ๋ฌธ์ž๋ˆ„์ ํ•ด์ฃผ๊ณ 
            if(count>1) answer += count.toString();
            // count๊ฐ€ 1๋ณด๋‹ค ํฌ๋ฉด ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์„œ answer์— ๋ˆ„์ 
            count = 1; // count๋Š” ๋‹ค์‹œ 1๋กœ ์ดˆ๊ธฐํ™”์‹œ์ผœ์ค€๋‹ค. 
          }
      }
      return answer;
}

let str = "KKHSSSSSSSE";
console.log(solution(str));

๋ฐ˜๋ณตํšŸ์ˆ˜๊ฐ€ 1์ธ๊ฒฝ์šฐ ์ƒ๋žตํ•˜๋ ค๋ฉด count ๊ฐ€ 1๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ์—๋งŒ ์ถœ๋ ฅํ•˜๋„๋กํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์จ์ฃผ์—ˆ๋‹ค. 

 

 

 

3. ์‚ฌ์šฉํ•œ ๊ฐœ๋…

Number.toString()

์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊ฟ”์ฃผ๊ธฐ

 

String(Number)

์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊ฟ”์ฃผ๊ธฐ

1. ๋ฌธ์ œ

ํ•œ ๊ฐœ์˜ ๋ฌธ์ž์—ด s์™€ ๋ฌธ์ž t๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ๋ฌธ์ž์—ด s์˜ ๊ฐ ๋ฌธ์ž๊ฐ€ ๋ฌธ์ž t์™€ ๋–จ์–ด์ง„ ์ตœ์†Œ๊ฑฐ๋ฆฌ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ž…๋ ฅ์„ค๋ช… |

์ฒซ ๋ฒˆ์งธ ์ค„์— ๋ฌธ์ž์—ด s์™€ ๋ฌธ์ž t๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋ฌธ์ž์—ด๊ณผ ๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 100์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

์ž…๋ ฅ์˜ˆ์ œ | teachermode e

์ถœ๋ ฅ์˜ˆ์ œ | 1 0 1 2 1 0 1 2 2 1 0

 

*๋งŒ์•ฝ ๋ฌธ์ œ๋ฅผ ์ฝ๊ณ ๋„ ์ดํ•ด๊ฐ€ ์•ˆ๊ฐ€๋ฉด, ์ž…๋ ฅ์˜ˆ์ œ์™€ ์ถœ๋ ฅ์˜ˆ์ œ๋ฅผ ๋ณด๋ฉด์„œ ์–ด๋–ป๊ฒŒ ๋‚˜์˜ค๋Š”๊ฑด์ง€ ์œ ์ถ”ํ•˜๋Š” ๊ฒƒ๋„ ๋ฌธ์ œํ•ด๊ฒฐ๋Šฅ๋ ฅ์ด๋‹ค. 

 

 

 

2. ์ ‘๊ทผ๋ฐฉ๋ฒ•

๋ฌธ์ž์—ด ์•ž์—์„œ๋ถ€ํ„ฐ ํƒ์ƒ‰, ๋’ค์—์„œ๋ถ€ํ„ฐ ํƒ์ƒ‰ ๋น„๊ตํ•ด์„œ ์ตœ์†Œ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๊ธฐ

 

๋ฐ˜๋ชฉ๋ฌธ์œผ๋กœ ๋ฌธ์ž์—ด ํ•˜๋‚˜์”ฉ ๋Œ๋ฉด์„œ ํƒ์ƒ‰

e๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ ์นด์šดํŒ…ํ•˜๋ฉด์„œ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค. 

๋ฐ˜๋Œ€๋กœ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋ฌธ์ž์—ด ํ•˜๋‚˜์”ฉ ๋Œ๋ฉด์„œ ํƒ์ƒ‰

e๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ ์นด์šดํŒ…ํ•˜๋ฉด์„œ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค. 

๊ทธ๋ฆฌ๊ณ  ๋น„๊ตํ•ด์„œ ์ตœ์†Œ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค. 

 

 

3. ๋ฌธ์ œํ’€์ด

e์—์„œ๋ถ€ํ„ฐ์˜ ๊ฐ€์žฅ ์งง์€ ๊ฑฐ๋ฆฌ๋ฅผ ๋ฆฌํ„ดํ•ด์•ผํ•œ๋‹ค. 

answer๋ฅผ ๋ฐฐ์—ด๋กœ ๋„ฃ๊ณ , ๊ฐ’์„ ๋ฐฐ์—ด์— pushํ•˜๊ธฐ๋กœ ํ•œ๋‹ค.

์ตœ๋Œ€๋ฌธ์ž์—ด์˜ ๊ธธ์ด 100์ด๋ฏ€๋กœ ๋ณ€์ˆ˜ p๋ฅผ ์ตœ๋Œ€๊ฐ’ 100์œผ๋กœ ์ž‘์„ฑํ•ด์ฃผ๊ธฐ๋กœ ํ•œ๋‹ค.

 

1. ๋ฌธ์ž e์—์„œ ์šฐ์ธก๋ฐฉํ–ฅ์œผ๋กœ์˜ ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๊ธฐ

for of ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ž์—ด์„ ๋Œ๋ฉด์„œ ๋ฌธ์ž๊ฐ€ e๊ฐ€ ์•„๋‹ˆ๋ฉด p++์„ ํ•ด์ค€๋‹ค.

e์ด๋ฉด 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. 

์ด ๋ฐฉ๋ฒ•์€ ๋ฌธ์ž e์—์„œ ์šฐ์ธก๋ฐฉํ–ฅ์œผ๋กœ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•œ ๊ฒƒ์ด๋‹ค.

 

2. ๋ฌธ์ž e์—์„œ ์ขŒ์ธก๋ฐฉํ–ฅ์œผ๋กœ์˜ ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๊ณ  ๋น„๊ต

๋‹ค์‹œ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋ฐ˜๋Œ€๋กœ ๋ˆ๋‹ค. 

๋ฐ˜๋Œ€๋กœ ๋Œ๋ฉด์„œ e์—์„œ ์ขŒ์ธก๋ฐฉํ–ฅ์œผ๋กœ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•œ๋‹ค. 

์ฆ‰, ์ขŒ์ธก์ด๋“  ์šฐ์ธก์ด๋“  ๋‘˜์ค‘์—์„œ ๊ฐ€์žฅ ๋” ์ž‘์€ ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•˜๋„๋ก ํ•œ๋‹ค. 

 

function solution(str, e){
	let answer = [];
    let p = 100; // ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 100์„ ๋„˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ตœ๋Œ€๊ฐ’ 100์œผ๋กœ ์„ค์ •ํ•ด์ค€๋‹ค. 
    for(x of str){
    	if(x === e){
        	p=0; // p๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ค€๋‹ค
            answer.push(p);
        } else {
        	p++;
            answer.push(p);
        }
    }
    p = 100; // ๋ฐ˜๋ณต๋ฌธ์„ ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ๋ณ€์ˆ˜ p ์›๋ž˜๋Œ€๋กœ ์ดˆ๊ธฐํ™”
    for(let i = str.length-1; i>=0; i--){
    	if(str[i] ===e){
        	p=0;
        } else {
        	p++;
            answer[i] = Math.min(answer[i], p);
            // ์ฒซ๋ฒˆ์งธ ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ๋“ค์–ด์žˆ๋Š” p์™€ ํ˜„์žฌ answer[i]๋ฅผ ๋น„๊ตํ•ด์„œ ์ž‘์€ ๊ฐ’์œผ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.
        }
     }
     return answer;
}

let str = "teachermode";
console.log(solution(str, 'e');

 

 

 

4. ์‚ฌ์šฉ๊ฐœ๋…

Math.min()

Math.min([value1[, value2[, )

์—ฌ๊ธฐ์„œ value๋Š” ์ˆซ์žํ˜•์ด์–ด์•ผํ•œ๋‹ค. 1๊ฐœ ์ด์ƒ์˜ ์ธ์ž๊ฐ’์ด ์ˆซ์žํ˜•์œผ๋กœ ๋ณ€ํ™˜์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ NaN ๋ฐ˜ํ™˜

๋ฌธ์ž์—ด์—์„œ ์ˆซ์ž๋งŒ ๊ฑธ๋Ÿฌ๋‚ด์„œ ์ถ”์ถœํ•˜๊ธฐ. ๋ฌธ์ž์—ด ์†์˜ ์ˆซ์ž๋ฅผ ์ˆซ์ž๋กœ ๋ฐ”๊ฟ”์ค˜์•ผํ•œ๋‹ค. 

 

๋ฌธ์ œ

๋ฌธ์ž์™€ ์ˆซ์ž๊ฐ€ ์„ž์—ฌ์žˆ๋Š” ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง€๋ฉด ๊ทธ ์ค‘ ์ˆซ์ž๋งŒ ์ถ”์ถœํ•˜์—ฌ ๊ทธ ์ˆœ์„œ๋Œ€๋กœ ์ž์—ฐ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๋งŒ์•ฝ “tge0a1h205er”์—์„œ ์ˆซ์ž๋งŒ ์ถ”์ถœํ•˜๋ฉด 0, 1, 2, 0, 5์ด๊ณ  ์ด๊ฒƒ์„ ์ž์—ฐ์ˆ˜๋ฅผ ๋งŒ๋“ค๋ฉด 1205์ด ๋ฉ๋‹ˆ๋‹ค. ์ถ”์ถœํ•˜์—ฌ ๋งŒ๋“ค์–ด์ง€๋Š” ์ž์—ฐ์ˆ˜๋Š” 100,000,000์„ ๋„˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

์ž…๋ ฅ์˜ˆ์ œ | g0en2T0s8eSoft

์ถœ๋ ฅ์˜ˆ์ œ | 208

 

 

์ ‘๊ทผ

๋ฌธ์ž์—ด ์•ˆ์—์„œ ๊ธฐํ˜ธ๋Š” ์ œ์™ธํ•˜๊ณ  ๋ฌธ์ž๋งŒ ๊ฒ€์‚ฌํ•˜์—ฌ ํŒฐ๋ฆฐ๋“œ๋กฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋˜ ๊ฒƒ ์ฒ˜๋Ÿผ

์ •๊ทœํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•ด์„œ ์ˆซ์ž๋งŒ ๊ฑธ๋Ÿฌ์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•ด์ค€๋‹ค. 

 

ํ’€์ด(1)

replace(/[^0-9]/g, ' ') -> ์ „์ฒด ์˜์—ญ์—์„œ 0-9๊นŒ์ง€๊ฐ€ ์•„๋‹Œ๊ฑธ ์ฐพ์•„๋ผ.

๊ทธ๋ฆฌ๊ณ  ๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ parseInt()๋ฅผ ์‚ฌ์šฉํ•ด์„œ answer๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. 

๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ๋ฐ”๊ฟ”์ค˜์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— parseInt()๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค. 

function solution(str){
            let answer;
            str = str.replace(/[^0-9]/g, '');
            answer = parseInt(str);
            return answer;
        }

let str = "g0en2T0s8eSoft";
console.log(solution(str));

 

ํ’€์ด(2)

isNaN(value) -> ์ˆซ์ž์ธ๊ฐ€ ์•„๋‹Œ๊ฐ€? ๋ฅผ ํ™•์ธํ•ด์„œ ๋ˆ„์ ํ•˜๋Š” ๋ฐฉ๋ฒ•

function solution(str){
	let answer = "";
    for(let x of str){
    	if(!isNaN(x)) answer+=x;
    }
    return parseInt(answer);
}

let str = "g0en2T0s8eSoft";
console.log(solution(str));

 

 

์‚ฌ์šฉํ•œ ๊ฐœ๋…

isNaN()

์ˆซ์ž์ธ์ง€ ์•„๋‹Œ์ง€ ํ™•์ธํ•˜๊ธฐ

isNaN(value)

์ˆซ์ž๊ฐ€ ์•„๋‹๊ฒฝ์šฐ true ๋ฆฌํ„ด, ์ˆซ์ž์ผ ๊ฒฝ์šฐ false ๋ฆฌํ„ดํ•œ๋‹ค. 

 

parseInt()

๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ 

parseInt(string)

string - ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•  ๋ฌธ์ž์—ด

ํšŒ๋ฌธ ๋ฌธ์ž์—ด = ํŒฐ๋ฆฐ๋“œ๋กฌ

ํšŒ๋ฌธ ๋ฌธ์ž์—ด(ํŒฐ๋ฆฐ๋“œ๋กฌ) : ์•ž์—์„œ๋ถ€ํ„ฐ ์ฝ์œผ๋‚˜ ๋’ค์—์„œ๋ถ€ํ„ฐ ์ฝ์œผ๋‚˜ ๊ฐ™์€ ๋ฌธ์ž์—ด.

 

๋ฌธ์ž์—ด ํƒ์ƒ‰ํ•˜๋Š” ๋ฌธ์ œ. ๋ฌธ์ž์—ด์— ์–ด๋– ํ•œ ์กฐ๊ฑด์„ ์ฃผ๊ณ  ํƒ์ƒ‰ํ•˜๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

 

1. ๋ฌธ์ œ

์•ž์—์„œ ์ฝ์„ ๋•Œ๋‚˜ ๋’ค์—์„œ ์ฝ์„ ๋•Œ๋‚˜ ๊ฐ™์€ ๋ฌธ์ž์—ด์„ ํŒฐ๋ฆฐ๋“œ๋กฌ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์ด ์ž…๋ ฅ๋˜๋ฉด ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ํŒฐ๋ฆฐ๋“œ๋กฌ์ด๋ฉด "YES", ์•„๋‹ˆ๋ฉด “NO"๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ๋‹จ ํšŒ๋ฌธ์„ ๊ฒ€์‚ฌํ•  ๋•Œ ์•ŒํŒŒ๋ฒณ๋งŒ ๊ฐ€์ง€๊ณ  ํšŒ๋ฌธ์„ ๊ฒ€์‚ฌํ•˜๋ฉฐ, ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์•ŒํŒŒ๋ฒณ ์ด์™ธ์˜ ๋ฌธ์ž๋Š” ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค. 

 

์ž…๋ ฅ์˜ˆ์ œ | found7, time: study; Yduts; emit, 7 Dnuof

์ถœ๋ ฅ์˜ˆ์ œ | YES

 

 

2. ์ ‘๊ทผ๋ฐฉ๋ฒ•

ํšŒ๋ฌธ ๋ฌธ์ž์—ด์„ ํ’€์—ˆ๋˜ ๋ฐฉ๋ฒ•๊ณผ ๊ฐ™๋‹ค.

ํšŒ๋ฌธ ๋ฌธ์ž์—ด์€ ๋ฌธ์ž์—ด ์•ˆ์—์„œ ์ œ์ผ ์•ž๊ณผ ์ œ์ผ ๋์ด ๊ฐ™๋‹ค๋Š” ๋ง์ด๊ณ ,

์•ž์—์„œ๋ถ€ํ„ฐ ํ•œ๊ธ€์ž์™€ ๋’ค์—์„œ ๋ถ€ํ„ฐ ํ•œ๊ธ€์ž๋ฅผ ๋น„๊ตํ•ด์•ผํ•œ๋‹ค. 

์•ŒํŒŒ๋ฒณ๋งŒ ๊ฐ€์ง€๊ณ  ํšŒ๋ฌธ ๊ฒ€์‚ฌ๋ฅผ ํ•ด์•ผํ•˜๋ฏ€๋กœ ๋ฌธ์ž์—ด์— ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. 

 

 

2. ๋ฌธ์ œ ํ’€์ด

1. ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ฒ˜์Œ๋ถ€ํ„ฐ ์†Œ๋ฌธ์ž or ๋Œ€๋ฌธ์ž๋กœ ํ†ต์ผ์‹œํ‚จ๋‹ค.

2. ์ •๊ทœํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ž์—ด ๋‚ด์— ์•ŒํŒŒ๋ฒณ์„ ์ œ์™ธํ•˜๊ณ  ๋‚˜๋จธ์ง€๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค. 

2. for ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋Œ๋ฉด์„œ ์•ž์—์„œ๋ถ€ํ„ฐ ๋ฌธ์ž์™€ ๋’ค์—์„œ๋ถ€ํ„ฐ ๋ฌธ์ž ์ง์ง€์–ด์„œ ๋น„๊ตํ•ด์ค€๋‹ค. 

function solution(str){
	let answer = "YES";
    str = str.toLowerCase().replace(/[^a-z]/g, '');
    // ์ฒ˜์Œ๋ถ€ํ„ฐ ์†Œ๋ฌธ์ž๋‚˜ ๋Œ€๋ฌธ์ž๋กœ ํ†ต์ผ์‹œํ‚จ๋‹ค.
    // ์†Œ๋ฌธ์ž๋กœ ํ†ต์ผํ•˜๊ณ  ์—ฌ๊ธฐ๋‹ค๊ฐ€ replace๋ฅผ ํ•ด์„œ ์†Œ๋ฌธ์ž๋งŒ ๋‘๊ณ  ๋‚˜๋จธ์ง€ ์ œ๊ฑฐ -> ์ •๊ทœํ‘œํ˜„์‹ ์‚ฌ์šฉ
	// ์ •๊ทœํ‘œํ˜„์‹์—์„œ ^ ํ‘œ์‹œ -> ๋ถ€์ • 
	// [^a-z/g] -> ์†Œ๋ฌธ์ž a-z๊นŒ์ง€ ์•„๋‹Œ๊ฑธ ์ฐพ์•„๋ผ global -> ์ „์ฒด ์˜์—ญ์—์„œ
    let len = str.length;
    for(let i=0; i<len; i++){
    	if(str[i] !== str[len-i-1]) return "NO";
    }
    return answer;
}

let str = "found7, time: study; Yduts; emit, 7Dnuof";
console.log(solution(str));
function solution(str){
	let answer = "YES";
    str = str.toLowerCase().replace(/[^a-z]/g, '');
    let len = str.length;
    for(let i=0; i<len; i++){
    	if(str !== str.split('').reverse('').join('')) return "NO";
    }
    return answer;
}

let str = "found7, time: study; Yduts; emit, 7Dnuof";
console.log(solution(str));

 

3. ์‚ฌ์šฉ๊ฐœ๋…

 

์ •๊ทœํ‘œํ˜„์‹

์ •๊ทœ์‹์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ• 1. ์ •๊ทœ์‹ ๋ฆฌํ„ฐ๋Ÿด์‚ฌ์šฉ (์Šฌ๋ž˜์‹œ / ๋กœ ๊ฐ์‹ธ๋Š” ํŒจํ„ด)

์ •๊ทœ์‹์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ• 2. RegExp ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•

๋ฌธ์ž์—ด ํƒ์ƒ‰ํ•˜๋Š” ๋ฌธ์ œ์ธ๋ฐ, ์–ด๋–ป๊ฒŒ ํƒ์ƒ‰ํ•ด์„œ ๋น„๊ตํ• ๊ฑด์ง€ ์ƒ๊ฐํ•ด๋ณด๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค. 

 

1. ๋ฌธ์ œ

์•ž์—์„œ ์ฝ์„ ๋•Œ๋‚˜ ๋’ค์—์„œ ์ฝ์„ ๋•Œ๋‚˜ ๊ฐ™์€ ๋ฌธ์ž์—ด์„ ํšŒ๋ฌธ ๋ฌธ์ž์—ด์ด๋ผ๊ณ  ํ•œ๋‹ค. ๋ฌธ์ž์—ด์ด ์ž…๋ ฅ๋˜๋ฉด ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ํšŒ๋ฌธ ๋ฌธ์ž์—ด์ด๋ฉด "YES", ํšŒ๋ฌธ ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๋ฉด “NO"๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ๋‹จ ํšŒ๋ฌธ์„ ๊ฒ€์‚ฌํ•  ๋•Œ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

์ž…๋ ฅ์˜ˆ์ œ | gooG

์ถœ๋ ฅ์˜ˆ์ œ | YES

 

 

2. ์ ‘๊ทผ๋ฐฉ๋ฒ• 

(1) ๋ฐฉ๋ฒ•

ํšŒ๋ฌธ ๋ฌธ์ž์—ด์€ ๋ฌธ์ž์—ด ์•ˆ์—์„œ ์ œ์ผ ์•ž๊ณผ ์ œ์ผ ๋์ด ๊ฐ™๋‹ค๋Š” ๋ง์ด๊ณ ,

์•ž์—์„œ๋ถ€ํ„ฐ ํ•œ๊ธ€์ž์™€ ๋’ค์—์„œ ๋ถ€ํ„ฐ ํ•œ๊ธ€์ž๋ฅผ ๋น„๊ตํ•ด์•ผํ•œ๋‹ค. 

๋ฌธ์ž์—ด ๋‚ด์—์„œ 2๊ฐœ์”ฉ ์ง์„ ์ง€์–ด ๋น„๊ตํ•˜๊ฒŒ๋˜๋‹ˆ length/2๋กœ ๋ฒ”์œ„๋ฅผ ์ •ํ•ด ๋ฐ˜๋ณตํ•œ๋‹ค. 

 

(2) ๋ฐฉ๋ฒ•

๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ž์—ด์„ ๋ฐ˜๋Œ€๋กœ ๋’ค์ง‘์–ด์„œ ์›๋ž˜ ๋ฌธ์ž์—ด๊ณผ ๋น„๊ตํ•œ๋‹ค. 

 

3. ๋ฌธ์ œํ’€์ด(1) 

1. ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ฒ˜์Œ๋ถ€ํ„ฐ ์†Œ๋ฌธ์ž o r๋Œ€๋ฌธ์ž๋กœ ํ†ต์ผ์‹œํ‚จ๋‹ค. 

2. for๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋ˆ๋‹ค.

3. ์•ž์—์„œ๋ถ€ํ„ฐ ๋ฌธ์ž์™€ ๋’ค์—์„œ๋ถ€ํ„ฐ ๋ฌธ์ž ์ง์„์ง€์–ด์„œ ํ™•์ธํ•˜๋ฏ€๋กœ

length/2๋กœ ๋‚˜๋ˆˆ ํšŸ์ˆ˜๋ฅผ ์ ์–ด์ค€๋‹ค.

4. ์กฐ๊ฑด ์•ž์—์„œ๋ถ€ํ„ฐ ๊ธ€์ž์™€ ๋’ค์—์„œ ๋ถ€ํ„ฐ ๊ธ€์ž ๋น„๊ตํ•ด์„œ ๋‹ค๋ฅด๋ฉด NO ๊ฐ™์œผ๋ฉด YES ๋ฆฌํ„ดํ•œ๋‹ค. 

 

function solution(str){
	let answer = "YES";
    str =  str.toLowerCase();
    let len = str.length;
    for(let i = 0; i<Math.floor(len/2); i++){
    	if(str[i] !== str[len-i-1]) return "NO";
    }
    return answer;
}

let str = "gooG";
console.log(solution(str));

 

3. ๋ฌธ์ œํ’€์ด(2)

๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. 

1. ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ฒ˜์Œ๋ถ€ํ„ฐ ์†Œ๋ฌธ์ž or ๋Œ€๋ฌธ์ž๋กœ ํ†ต์ผ์‹œํ‚จ๋‹ค.

2. ์›๋ž˜ ๋ฌธ์ž์—ด๊ณผ ๋ฌธ์ž์—ด์„ ๊ฑฐ๊พธ๋กœ ๋’ค์ง‘์€ ๊ฒƒ์„ ๋น„๊ตํ•ด์„œ ๊ฐ™์œผ๋ฉด YES ๋‹ค๋ฅด๋ฉด NO ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

3. ๋ฌธ์ž์—ด์„ String.split() ๋ฉ”์†Œ๋“œ๋กœ ํ•˜๋‚˜์”ฉ ๋–จ์–ดํŠธ๋ ค์„œ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค๊ณ , 

Array.reverse() ๋ฉ”์†Œ๋“œ๋กœ ๋’ค์ง‘๋Š”๋‹ค. Array.join() ๋ฉ”์†Œ๋“œ๋กœ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ ๋‹ค. 

 

 

String.split()

๋นˆ๋ฌธ์ž์—ด์„ ๋„ฃ๊ณ  splitํ•˜๋ฉด ๋ฌธ์ž ์š”์†Œ๊ฐ€ ํ•˜๋‚˜์”ฉ ๋‚˜๋ˆ„์–ด์ ธ์„œ ๋ฐฐ์—ด์ด ๋œ๋‹ค.

๋ฐฐ์—ดํ™”๊ฐ€ ๋˜์–ด์•ผ reverse ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

Array.reverse()

๋ฐฐ์—ด์˜ ์ˆœ์„œ๋ฅผ ๊ฑฐ๊พธ๋กœ ๋งŒ๋“œ๋Š” ๋ฉ”์†Œ๋“œ

 

Array.join()

๋ฐฐ์—ด์„ string์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ

 

function solution(str){
	let answer = "YES";
    str = str.toLowerCase();
    if(str.split('').reverse('').join('') !== str) return "NO"
    return answer;
}

let str = "gooG";
console.log(solution(str));

 

 

+ Recent posts