Skip to content

Instantly share code, notes, and snippets.

@terremoth
Last active Dec 17, 2018
Embed
What would you like to do?
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