[알고리즘] LeetCode - Valid Palindrome

Jerry·2021년 1월 22일
0

LeetCode

목록 보기
7/73
post-thumbnail

LeetCode - Valid Palindrome

문제 설명

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Note

For the purpose of this problem, we define empty string as valid palindrome.

Example 1

Input: "A man, a plan, a canal: Panama"
Output: true

Example 2

Input: "race a car"
Output: false

Constraint

  • s consists only of printable ASCII characters.

Solution

/**
 * @param {string} s
 * @return {boolean}
 */
let isPalindrome = function(s) {
    s = s.toLowerCase(); // 대문자 -> 소문자
    const regexp = new RegExp(/[^a-z0-9]/g); //알파벳이 아니면. g: 모든 매칭되는 포인트에 적용
    
    s=s.replace(regexp, '');

    let alpLastIdx = s.length-1;

    for (let i = 0; i <= (alpLastIdx / 2) >> 0; i++){

        if (s[i] != s[alpLastIdx - i]) {
            return false;
        }
    }
    return true;
};

즐건 주말 ~.~

profile
제리하이웨이

0개의 댓글