javascript solution 132 ms


  • 0
    I
    /**
     * @param {string} s
     * @return {boolean}
     */
    var isPalindrome = function(s) {
        s= s.replace(/\W/g, '').toLowerCase();
        if(s===""){return true}
        var i=0;
        var j = s.length-1;
        
        while(i<j){
        
            if(s[i]!==s[j]){
                return false
            }
            else{
                i++;
                j--;
            }      
     
    
        }
        
        return true
        
    };
    

  • 0
    X

    I am just curious, is str.replace() considered a low performance function?


  • 0

    @xiangyu6 it's O(n) in the length of the string being searched. You generally won't see performance hits unless the regex is complex or you are nesting it in one or more loops. Here it does not change the overall complexity.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.