Created
July 14, 2020 05:53
-
-
Save ultramarshall/63440d1f8d6cb3bb5409320461bb2166 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
<?php | |
$string = 'ABDCKDHJABDCBDAUOQJDBADCLDLCHBCBABCBAABCDAJDBABDCABDABDBCADBCASSJGABCDAUTACBDBQWUDNCDBCADKDHABDJGBDABCBDBADCACADBADBCBAD'; | |
$pattern = 'ABCD'; | |
$total_match = 0; | |
function is_anagram($a, $b) { | |
return(count_chars($a, 1) == count_chars($b, 1)); | |
} | |
//var_dump(is_anagram('ab','ba')); | |
for($i=0; $i< strlen($string); $i++) { | |
$substr = substr($string,$i,4); | |
if(strlen($substr)==4) { | |
if(is_anagram($substr,$pattern)) { | |
echo 'Index ' . $i . ' : ' . $substr; | |
echo '<br>'; | |
$total_match++; | |
} | |
} | |
} | |
echo '<br>'; | |
echo 'Total match count : ' . $total_match; | |
/******************************************************** | |
** OUTPUT *********************************************** | |
******************************************************** | |
Index 0 : ABDC | |
Index 8 : ABDC | |
Index 11 : CBDA | |
Index 20 : BADC | |
Index 37 : ABCD | |
Index 38 : BCDA | |
Index 45 : ABDC | |
Index 46 : BDCA | |
Index 47 : DCAB | |
Index 48 : CABD | |
Index 54 : DBCA | |
Index 55 : BCAD | |
Index 56 : CADB | |
Index 57 : ADBC | |
Index 58 : DBCA | |
Index 66 : ABCD | |
Index 67 : BCDA | |
Index 73 : ACBD | |
Index 84 : DBCA | |
Index 85 : BCAD | |
Index 98 : DABC | |
Index 104 : BADC | |
Index 109 : CADB | |
Index 113 : ADBC | |
Index 116 : CBAD | |
Total match count : 25 | |
*/ | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment