Created
February 21, 2018 19:47
-
-
Save NicholasEli/3b0c2b363a2400cd8a8e7b37c9d3416d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function stringPosition(needle, haystack) { | |
var m = needle.length, | |
n = haystack.length; | |
if (!m || !n || m > n) { | |
return -1; | |
} | |
if (m === n) { | |
return needle === haystack ? 0 : -1; | |
} | |
for (var j = 0; j < n; j++) { | |
for (var i = 0; i < m; i++) { | |
if (needle[i] !== haystack[i + j]) { | |
break; | |
} | |
if (i === m - 1) { | |
return j; | |
} | |
} | |
} | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment