I try to calculate several “bands”, in particular the greatest number of wins and losses in a row, but also most cases of games without a win, without a loss.
I have a line that looks like this: 'WWWDDWWWLLWLLLL'
To do this, I need to return:
- The longest sequential start of the W charector (then I replicate for L)
- The longest sequential start without a W charector (then I replicate for L)
I found and adapted the next one that will go through my array and tell me the longest sequence, but I cannot adapt it to fit the above criteria.
All help and training was highly appreciated :)
function getLongestSequence($sequence){
$sl = strlen($sequence);
$longest = 0;
for($i = 0; $i < $sl; )
{
$substr = substr($sequence, $i);
$len = strspn($substr, $substr{0});if($len > $longest)
$longest = $len;
$i += $len;
}
return $longest;
}
echo getLongestSequence($sequence);