유효한 팰린드롬

WooBuntu·2021년 2월 17일
0

JS 90제

목록 보기
8/33
  • 내 풀이
const solution = string => {
  string = string.toLowerCase().replace(/[^a-z]/g, '');
  const length = string.length;
  for (let i = 0; i < Math.floor(length - 1 / 2); i++) {
    if (string[i] != string[length - 1 - i]) {
      return 'NO';
    }
  }
  return 'YES';
};

const result = solution('found7, time: study; Yduts; emit, 7Dnuof');
console.log(result);
  • 강의 풀이
function solution(s) {
  let answer = 'YES';
  s = s.toLowerCase().replace(/[^a-z]/g, '');
  console.log(s);
  if (s.split('').reverse().join('') !== s) return 'NO';
  return answer;
}

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

0개의 댓글