Created
June 6, 2019 06:18
-
-
Save anushshukla/ab7b938e8b1e6503884aa7aee6b9d035 to your computer and use it in GitHub Desktop.
Get Appearance Count
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 appearanceCount($str1len, $str2len, $str1, $str2) | |
{ | |
$matches = $chunk = $i = 0; | |
$str1len = strlen($str1); | |
$str2len = strlen($str2); | |
for($i; $i <= $str2len; $i++ ) { | |
if(isset($matched)) { | |
$chunk = $matched === 4 ? $chunk + 1 : $chunk + $matched; | |
} | |
var_dump('chunk',$chunk); | |
$substr = substr($str2, $chunk, $str1len); | |
if(!$substr) { | |
break; | |
} | |
$lastMatchedKey = $matched = 0; | |
$strtoMatchArray = str_split($str1); | |
$substrArr = str_split($substr); | |
var_dump($substr); | |
foreach ($substrArr as $key => $character) { | |
if(!in_array($character, $strtoMatchArray) || ($lastMatchedKey && $lastMatchedKey !== $key-1)) { | |
$strtoMatchArray = str_split($str1); | |
$lastMatchedKey = $matched = 0; | |
continue; | |
} | |
$lastMatchedKey = $key; | |
++$matched; | |
unset($strtoMatchArray[array_flip($strtoMatchArray)[$character]]); | |
var_dump($matched); | |
if($matched === $str1len) { | |
var_dump('matches'); | |
$matches++; | |
} | |
} | |
} | |
return $matches; | |
} | |
echo appearanceCount(4,13,'cAda','AEAcdacdAcaqt'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment