-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented the Knuth Morris Pattern Matching Algorithm. (#2702)
- Loading branch information
1 parent
6ce77da
commit 3751213
Showing
1 changed file
with
86 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
// Function to perform the LPS (Longest Proper Prefix Suffix) | ||
function ComputeLPS(string $pattern, array &$lps) { | ||
|
||
$len = 0; | ||
$i = 1; | ||
$M = strlen($pattern); | ||
$lps[0] = 0; | ||
|
||
while ($i < $M) { | ||
if ($pattern[$i] == $pattern[$len]) { | ||
$len++; | ||
$lps[$i] = $len; | ||
$i++; | ||
} else { | ||
if ($len != 0) { | ||
$len = $lps[$len - 1]; | ||
} else { | ||
$lps[$i] = 0; | ||
$i++; | ||
} | ||
} | ||
} | ||
} | ||
|
||
function KMPStringMatching(string $str, string $pattern): array { | ||
|
||
$matches = []; | ||
$M = strlen($pattern); | ||
$N = strlen($str); | ||
$i = $j = 0; | ||
$lps = []; | ||
|
||
ComputeLPS($pattern, $lps); | ||
|
||
while ($i < $N) { | ||
|
||
if ($pattern[$j] == $str[$i]) { | ||
$j++; | ||
$i++; | ||
} | ||
|
||
if ($j == $M) { | ||
array_push($matches, $i - $j); | ||
$j = $lps[$j - 1]; | ||
} else if ($i < $N && $pattern[$j] != $str[$i]) { | ||
if ($j != 0) | ||
$j = $lps[$j - 1]; | ||
else | ||
$i = $i + 1; | ||
} | ||
} | ||
|
||
return $matches; | ||
} | ||
|
||
$txt = "AABAACAADAABABBBAABAA"; | ||
$pattern = "AABA"; | ||
// Performing the Pattern Matching Operation. | ||
$matches = KMPStringMatching($txt, $pattern); | ||
|
||
if ($matches) { | ||
foreach ($matches as $pos) { | ||
echo "Pattern found at index : " . $pos . "\n"; | ||
} | ||
} | ||
|
||
/* | ||
Complexity for the Algorithm : O(N + M) | ||
Where O(M) is for Computing LPS | ||
O(N) is for Computing KMP Algorithm. | ||
*/ | ||
|
||
/* | ||
INPUT: | ||
$txt = "AABAACAADAABABBBAABAA"; | ||
$pattern = "AABA"; | ||
OUTPUT: | ||
Pattern found at index : 0 | ||
Pattern found at index : 9 | ||
Pattern found at index : 16 | ||
*/ | ||
?> |