Last active
May 17, 2016 17:40
-
-
Save TheRealJAG/602cf10ed21f57c2f918bdf3be9e4c76 to your computer and use it in GitHub Desktop.
Longest Run
This file contains hidden or 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 | |
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