Created
November 21, 2017 01:36
-
-
Save lastday154/09bab8b9d0cfff370df939d0ae46f216 to your computer and use it in GitHub Desktop.
Strstr PHP implementation
This file contains 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
<?php | |
function search(string $pattern, string $text): int | |
{ | |
$patternLength = strlen($pattern); | |
$textLength = strlen($text); | |
for ($i=0; $i< $textLength-$patternLength; $i++) { | |
for ($j=0; $j < $patternLength; $j++) { | |
if ($pattern[$j] != $text[$i+$j]) { | |
break; | |
} | |
} | |
if ($j == $patternLength) { | |
return $i; | |
} | |
} | |
return -1; | |
} | |
$text = "ACAADAABAAABAAAABA"; | |
$pattern = "AABA"; | |
echo search($pattern, $text); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment