Created
November 20, 2017 10:40
-
-
Save restart916/3eeacc03572b1024379226d9f06f0756 to your computer and use it in GitHub Desktop.
This file contains 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 | |
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