Solution using XOR


  • 0
    R
    var strStr = function(haystack, needle) {
        var i,
            j,
            found;
            
        haystack = haystack.split("");
        needle = needle.split("");
        
        for(i = 0; i <= (haystack.length - needle.length); i++){
            found = 1;
            for(j = 0; j < needle.length; j++){
                if((haystack[i + j].charCodeAt(0) ^ needle[j].charCodeAt(0)) !== 0){
                    found = 0;
                    break;
                }
            }
            if(found === 1)
                return i;
        }
        
        return -1;
        
    };

Log in to reply
 

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