Skip to content

Instantly share code, notes, and snippets.

@kazua
Created February 17, 2014 12:38
Show Gist options
  • Save kazua/9049833 to your computer and use it in GitHub Desktop.
Save kazua/9049833 to your computer and use it in GitHub Desktop.
世界で闘うプログラマ本の1-5 by PHP
<?php
//write kazua
//世界で闘うプログラマ本1-5 by PHP
//test
$a = new worldproblem1_5();
echo $a->zip("kkkksssllllllllllllssssaaaaaaaaa").PHP_EOL;
echo $a->zip("sssswwwweeeddfffffff").PHP_EOL;
echo $a->zip("aweyerhhhhhhferyerrrrrrr").PHP_EOL;
echo $a->zip("sdfsdfsdgrgegeh").PHP_EOL;
class worldproblem1_5{
function zip($str){
$cnt = 1;
$bf = $str[0];
$zstr = "";
for($i =1;$i < strlen($str);$i++){
if($str[$i] === $bf) $cnt++;
else{
$zstr .= $bf.$cnt;
$bf = $str[$i];
$cnt = 1;
}
}
if (strlen($str) < strlen($zstr.$bf.$cnt)) return $str;
else return $zstr.$bf.$cnt;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment