-
-
Save dikiwidia/c8bb48a3e544b0d99de4a96ba622c172 to your computer and use it in GitHub Desktop.
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
/*-------------------------------------------------------------------- | |
Moch Diki Widianto - PT BUMI LANCANG KUNING PUSAKA | |
Soal | |
Cari kemungkinan banyaknya pola yang dapat ditemukan | |
dari pattern "ABCD" | |
Contoh: | |
"ABCDASABCDSA" | |
index 0 "ABCD" | |
index 1 "BCDA" | |
index 6 "ABCD" | |
Total pola 3 | |
--------------------------------------------------------------------*/ | |
<?php | |
$string = "ACBKABSSCKCBACBKKCACBCBAACKCKCCABCJKCABBCAKBCADDDHEBEJHUITYIHCOIJAOABCDCDDABBCDACCDACBCDABCDAJABJKJFBENCBEYCBAIBCSHBHDJHBXBHSDCBHDBHCDCBABD"; | |
$pattern = "ABCD"; | |
$arr_pattern = []; | |
for ($i=0; $i < strlen($pattern); $i++) { | |
array_push($arr_pattern, $pattern[$i]); | |
} | |
$count = 0; | |
for ($i=0; $i <= (strlen($string) - strlen($pattern)); $i++) { | |
$arr_search = []; | |
for ($j=0; $j < count($arr_pattern); $j++) { | |
array_push($arr_search, $string[$i+$j]); | |
} | |
$found = 0; | |
foreach (array_unique($arr_search) as $value) { | |
if(in_array($value, $arr_pattern)){ | |
$found++; | |
} | |
} | |
if ($found == count($arr_pattern)){ | |
echo "<pre>"; | |
print_r($arr_search); | |
echo "</pre>"; | |
$count++; | |
} | |
} | |
echo "Total Pola: {$count}"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment