• Blog
  • Projects
  • Resume
profile_image

[Algorithm] LeetCode : Valid Palindrome, Valid Palindrome II

Algorithm

2021.05.28

알고리즘 기초 다지기 프로젝트 (feat. 코드없는 프로그래밍) [2021년 05월 28일]



Leetcode - Valid Palindrome

문제: LeetCode - 125. Valid Palindrome

코드

  • 코드 (1) : 통과 성공
/**
   * @param {string} s
   * @return {boolean}
   */
const isPalindrome = (s) => {
  if (!s) return true;
  // 알파벳, 숫자 제외하고 모두 제거
  s = s
    .replace(
      /[^a-zA-z\d]|[\{\}\[\]\/?.,;:|\)*~`!^\-+<>@\#$%&\\\=\(\'\"\_]/g,
      '',
    )
    .toLowerCase();

  let left = 0;
  let right = s.length - 1;

  while (left < right) {
    const strLeft = s[left];
    const strRight = s[right];
    if (strLeft === strRight) {
      left++;
      right--;
    } else return false;
  }
  return true;
};
  • 정규표현식 활용 + 투 포인터

Leetcode - Valid Palindrome II

문제: LeetCode - 680. Valid Palindrome II

코드

  • 코드 (1) : 통과 성공
/**
   * @param {string} s
   * @return {boolean}
   */
const validPalindrome = (s) => {
  if (!s) return true;

  // 알파벳, 숫자 제외하고 모두 제거
  s = s
    .replace(
      /[^a-zA-z\d]|[\{\}\[\]\/?.,;:|\)*~`!^\-+<>@\#$%&\\\=\(\'\"\_]/g,
      '',
    )
    .toLowerCase();

  let left = 0;
  let right = s.length - 1;
  while (left < right) {
    const strLeft = s[left];
    const strRight = s[right];

    if (strLeft !== strRight)
      return (
        oneMoreCheck(s, left + 1, right) ||
        oneMoreCheck(s, left, right - 1)
      );
    left++;
    right--;
  }
  return true;
};

const oneMoreCheck = (s, left, right) => {
  while (left < right) {
    const strLeft = s[left];
    const strRight = s[right];

    if (strLeft !== strRight) return false;
    left++;
    right--;
  }
  return true;
};


참고 자료

강의