나만보는개발공부블로그

Valid Palindrome 본문

Algorithms/leetcode

Valid Palindrome

alexrider94 2021. 3. 19. 12:01

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

 

Example 1:

Input: s = "A man, a plan, a canal: Panama"

Output: true

Explanation: "amanaplanacanalpanama" is a palindrome.

Example 2:

Input: s = "race a car"

Output: false

Explanation: "raceacar" is not a palindrome.

 

var isPalindrome = function(s) {
    let d = s.toLowerCase().replace(/[^a-z0-9]/g,"");

    let f = 0 , b = d.length-1;
    for(let i = 0 ; i<d.length; ++i){
        if(d[f] !== d[b]) return false;
        f++;
        b--;
    }
    return true;
};

'Algorithms > leetcode' 카테고리의 다른 글

Reverse Linked List  (0) 2021.03.23
Implement strStr()  (0) 2021.02.17
merge-two-sorted-lists  (0) 2021.02.11
Climbing Stair  (0) 2021.02.10
Longest Common Prefix  (0) 2021.02.09