kokoball의 devlog
article thumbnail
728x90



문제

https://www.acmicpc.net/problem/12904

문제 해석

처음엔 재귀 방식으로 가볍게 도전했다가 처참하게 깨졌다...

S의 길이가 999, T의 길이가 1000이기 때문에 재귀는 시간 초과가 발생한다.

 

해결 방법은 최대 길이가 T이기 때문에 T에서 하나씩 제거하며 결국 S와 길이가 같아졌을 때 비교하면 해결할 수 있다.

 

풀이

const filePath = process.platform === "linux" ? "dev/stdin" : "../test.txt";
const input = require("fs").readFileSync(filePath).toString().trim().split("\n");

function solution(input) {
let a = input[0].trim().split("");
let b = input[1].trim().split("");
let answer = 0;
  
while (1) {
  if (a.length === b.length) {
    if (isSameArray(a, b)) answer = 1;
    break;
  }
  if (b[b.length - 1] === "A") b.pop();
  else {
    b.pop();
    b.reverse();
  }
}
  
function isSameArray(arr1, arr2) {
  for (let i = 0; i < arr1.length; i++) {
    if (arr1[i] !== arr2[i]) return false;
    else continue;
  }
  return true;
}
  
  return answer;
}

console.log(solution(input));

 

 

728x90
profile

kokoball의 devlog

@kokoball-dev

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!