Skip to content

Instantly share code, notes, and snippets.

@TheRealJAG
Last active May 17, 2016 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheRealJAG/602cf10ed21f57c2f918bdf3be9e4c76 to your computer and use it in GitHub Desktop.
Save TheRealJAG/602cf10ed21f57c2f918bdf3be9e4c76 to your computer and use it in GitHub Desktop.
Longest Run
<?php
class Run
{
public static function indexOfLongestRun($str)
{
$strlen = strlen( $str );
$strlen=$strlen-1;
$maxIndex = 0;
$maxLength = 1;
$currentChar = $str[0];
$currentIndex = 0;
$currentLength = 1;
for( $i = 0; $i <= $strlen; $i++ ) {
$sub = $i - 1;
$currentChar = substr( $str, $sub, 1 );
if ($str[$i] == $currentChar) {
$currentLength++;
} else {
if ($maxLength < $currentLength) {
$maxIndex = $currentIndex;
$maxLength = $currentLength;
}
$currentLength = 1;
$currentIndex = $i;
$currentChar = $str[$i];
}
}
if ($maxLength < $currentLength)
$maxIndex = $currentIndex;
return $maxIndex;
}
}
// For testing purposes (do not submit uncommented):
/*
echo Run::indexOfLongestRun('abbcccddddcccbba');
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment