Skip to content

Instantly share code, notes, and snippets.

@terremoth
Last active December 17, 2018 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terremoth/d850eb370a31acfc85e139345fd7a0c8 to your computer and use it in GitHub Desktop.
Save terremoth/d850eb370a31acfc85e139345fd7a0c8 to your computer and use it in GitHub Desktop.
Count repeated chars sequence and build a string with its chars (only 1) + repeated numbers
<?php
$input = 'aaacccbdd';
$output = 'a3b1c3d2';
$list = str_split($input);
$matchs = [];
foreach ($list as $i => $char) {
isset($matchs[$char]) ? $matchs[$char]++ : $matchs[$char] = 1;
}
ksort($matchs);
$builded = '';
foreach ($matchs as $i => $num) {
$builded .= $i.$num;
}
var_dump($builded);
var_dump($builded === $output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment