Skip to content

Instantly share code, notes, and snippets.

@restart916
Created November 20, 2017 10:40
Show Gist options
  • Save restart916/3eeacc03572b1024379226d9f06f0756 to your computer and use it in GitHub Desktop.
Save restart916/3eeacc03572b1024379226d9f06f0756 to your computer and use it in GitHub Desktop.
<?php
function test($input)
{
$inputArr = str_split($input);
$result = [];
array_push($result, [
'char' => $inputArr[0],
'count' => 0
]);
foreach($inputArr as $char)
{
$last = count($result) - 1;
if ($result[$last]['char'] == $char) {
$result[$last]['count']++;
} else {
array_push($result, [
'char' => $char,
'count' => 1
]);
}
}
$last = count($result) - 1;
if($result[0]['char'] == $result[$last]['char']) {
$result[$last]['count'] += $result[0]['count'];
} else {
array_push($result, $result[0]);
}
array_shift($result);
foreach($result as $item) {
print($item['char'].$item['count']);
}
print(' ');
}
$input = 'bbaabaccchhaa';
test($input);
$input = 'abaccchhaa';
test($input);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment