How to get the reverse of that pair value from string? not event count within the square bracket.

A system supports StrangeID strings, if the string has a “Binary Address Authenticator Bytes”,
commonly called the “BAAB“. A BAAB is any four-character sequence, which consists of a pair of
two different characters followed by the reverse of that pair, such as “pqqp“, “xzzx”, “xoox”,”adda”,
etc. However, a supported StrangeID also must NOT have a BAAB within any [square bracket]
sequences. There can be many [square bracket] sequences in one StrangeID.

<?php
$data = "kqzzbaabzsjud[qgmydefzsujkcffws]testdeed";
function getNumberReversePair($string=''){

$totalCount=0;
 if(!empty($string)){
           $regex =  '/\[[^][]+]\K|\w+/m';
           preg_match_all($regex, $string, $matches);
           $filterString= implode(' ',array_filter($matches[0]));
  
     if(count($filterString)>0){
          $regex = '/(\w)(?!\1)(\w)\2\1/';
          preg_match_all($regex, $filterString, $matchesCount);
          //[baab][deed]
          $totalCount=count($matchesCount[0]);
     }
 }
   return $totalCount;     
}

echo "Total Reverse Pair is: ".getNumberReversePair($data);
?>
Output : Total Reverse Pair is: 2 

Leave a Reply

Your email address will not be published. Required fields are marked *