2

I am trying to calculate a few 'streaks', specifically the highest number of wins and losses in a row, but also most occurences of games without a win, games without a loss.

I have a string that looks like this; 'WWWDDWWWLLWLLLL'

For this I need to be able to return:

  • Longest consecutive run of W charector (i will then replicate for L)
  • Longest consecutive run without W charector (i will then replicate for L)

I have found and adapted the following which will go through my array and tell me the longest sequence, but I can't seem to adapt it to meet the criteria above.

All help and learning greatly 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);
danmullen
  • 2,556
  • 3
  • 20
  • 28
user3061608
  • 21
  • 1
  • 7
  • possible duplicate of [How to get length of the longest sequence with same characters in a string?](http://stackoverflow.com/questions/2803803/how-to-get-length-of-the-longest-sequence-with-same-characters-in-a-string) – Valentin Rodygin Nov 06 '14 at 09:49
  • Try regular expressions, specifically, you're looking for `W+` and `[^W]+`. – georg Nov 06 '14 at 09:55
  • Thanks for the replies. @ValentinRodygin, I have seen this but i can't see how to reference a specific charector, or look for any string excluding a specific charector. georg perhaps reg ex could be incorporated in it? – user3061608 Nov 06 '14 at 10:02

2 Answers2

4

You can use a regular expression to detect sequences of identical characters:

$string = 'WWWDDWWWLLWLLLL';
// The regex matches any character -> . in a capture group ()
// plus as much identical characters as possible following it -> \1+
$pattern = '/(.)\1+/';

preg_match_all($pattern, $string, $m);
// sort by their length
usort($m[0], function($a, $b) {
    return (strlen($a) < strlen($b)) ? 1 : -1;
});

echo "Longest sequence: " . $m[0][0] . PHP_EOL;
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Thanks for this - it works, but I didn't mention that the site I'm trying to implement on is currently on PHP 5.2 where it fails, any advice for this please? – user3061608 Nov 06 '14 at 10:12
  • Oh, i see. PHP5.2 does not support anonymous functions (as used by `usort` in the example above) I would suggest to update PHP on that server. However, here comes an example which works with all version of php : http://3v4l.org/Ckdl6 – hek2mgl Nov 06 '14 at 10:18
  • Thanks for your help, i now get thrown another warning: Warning: usort() [function.usort]: Invalid comparison function I am then shown the longest sequence as WWW – user3061608 Nov 06 '14 at 10:31
  • 1
    Probably the function should explicitly return `0` if `$a` and `$b` have the same length. Can you try that? – hek2mgl Nov 06 '14 at 11:44
  • Thank you, i have found a way of outputting the length of longest sequence of a specific character. But can i use preg_match_all to find longest part of string where specific charactor is not found? E.g. if specific charector is W i want to find longest number of charectors where it is no present... – user3061608 Nov 06 '14 at 11:57
2

You can achieve the maximum count of consecutive character in a particular string using the below code.

       $string = "WWWDDWWWLLWLLLL";
        function getLongestSequence($str,$c) {
        $len = strlen($str);
        $maximum=0;
        $count=0;
        for($i=0;$i<$len;$i++){
            if(substr($str,$i,1)==$c){
                $count++;
                if($count>$maximum) $maximum=$count;
            }else $count=0;
        }
        return $maximum;
        }
        $match="W";//change to L for lost count D for draw count
        echo getLongestSequence($string,$match);
rahul patel
  • 262
  • 1
  • 2
  • 15