- max: documentation ( source)
- strtolower: documentation ( source)
- ord: documentation ( source)
<?php
class test
{
public static function repeatedCharCount($string)
{
$lcString = strtolower($string);
$prevChar = $lcString[0];
$currentLen = 0;
$maxLen = 0;
for ($i = 1; $i < strlen($lcString); $i++) {
if ($lcString[$i] === $prevChar) {
$currentLen += 1;
} else {
$currentLen = 1;
}
$maxLen = max($maxLen, $currentLen);
$prevChar = $lcString[$i];
}
return $maxLen;
}
public static function consecutiveCharCount($string)
{
$lcString = strtolower($string);
$prevChar = $lcString[0];
$currentLen = 0;
$maxLen = 0;
$ascending = null;
for ($i = 1; $i < strlen($lcString); $i++) {
$diff = ord($lcString[$i]) - ord($prevChar);
if ($diff == 1) {
if ($ascending !== true) {
$ascending = true;
$currentLen = 1;
}
$currentLen += 1;
} elseif ($diff == -1) {
if ($ascending !== false) {
$ascending = false;
$currentLen = 1;
}
$currentLen += 1;
} else {
$currentLen = 1;
}
$maxLen = max($maxLen, $currentLen);
$prevChar = $lcString[$i];
}
return $maxLen;
}
}
echo test::consecutiveCharCount("abcdegionoas");
echo test::consecutiveCharCount("EDCb00808asabc");
echo test::consecutiveCharCount("asg7898");
echo test::consecutiveCharCount("asgab876567asfon");
echo test::repeatedCharCount("abcdegionoas");
echo test::repeatedCharCount("EDCb00808asabc");
echo test::repeatedCharCount("asg7898");
echo test::repeatedCharCount("asgaaa656bBBb7asfon");