fork download
  1. <?php
  2. $input = 'aaaabbcccff';
  3. $acv = array_count_values(str_split($input));
  4. arsort($acv);
  5. $arr = [];
  6. foreach($acv as $key=>$value)
  7. $arr[] = str_split(str_repeat($key, $value));
  8. $out = null;
  9. for($i=0; $i<count($arr[0]); $i++)
  10. for($j=0; $j<count($arr); $j++)
  11. $out.= isset($arr[$j][$i]) ? $arr[$j][$i] : null;
  12.  
  13. echo 'Input: '.$input.PHP_EOL.'Out: '.$out;
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
Input: aaaabbcccff
Out: acfbacfbaca