PHP Largest String
function largestString(string $s): string
{
$len = strlen($s);
$cur = '';
for ($i = $len - 1; $i >= 0; $i--) {
$cur = $s[$i] . $cur;
if (strlen($cur) === 3) {
if ($cur === 'abb') {
$s[$i] = 'b';
$s[$i + 1] = 'a';
$s[$i + 2] = 'a';
if (isset($s[$i + 4]) && $s[$i + 4] === 'b') {
$i += 4 + 1;
} elseif (isset($s[$i + 3]) && $s[$i + 3] === 'b') {
$i += 3 + 1;
} elseif ($s[$i + 2] === 'b') {
$i += 2 + 1;
}
}
if ($s[$i + 1] === 'b') {
$i += 1 + 1;
} else {
++$i;
}
$cur = '';
}
}
return $s;
}
This builds the biggest valid string it can under the challenge rules by always choosing the best next character it is allowed to use.