Skip to content

Instantly share code, notes, and snippets.

@mochdikiwidianto
Created December 23, 2022 23:28
Show Gist options
  • Save mochdikiwidianto/4a300594c1e8bf640cb8be658f6a5920 to your computer and use it in GitHub Desktop.
Save mochdikiwidianto/4a300594c1e8bf640cb8be658f6a5920 to your computer and use it in GitHub Desktop.
/*--------------------------------------------------------------------
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