Skip to content

Instantly share code, notes, and snippets.

@novaknole
Created October 5, 2020 18:14
Show Gist options
  • Save novaknole/84cbff824012390ca610b84f2c6c24fa to your computer and use it in GitHub Desktop.
Save novaknole/84cbff824012390ca610b84f2c6c24fa to your computer and use it in GitHub Desktop.
function solution($S, $K) {
// write your code in PHP7.0
print "$S, $K<br>\n";
$min = "";
for($i=0; $i<=strlen($S)-$K-1; $i++) {
$n = $i+$K;
$string = substr($S,0,$i).substr($S,$n);
//print "new string $i $string\n";
$notation = notation($string);
$len = strlen($notation);
if($len<$min || $min=="") {
$min = $len;
$min_notation = $notation;
}
}
return $min;
}
function notation($S) {
$prev = "";
$cnt = 1;
$notation = "";
for($i=0; $i<=strlen($S); $i++) {
$x = substr($S,$i,1);
if($x==$prev) {
$cnt++;
} else {
$notation .= $prev;
if($cnt>1) {
$notation .= $cnt;
}
$cnt = 1;
}
$prev = $x;
}
return $notation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment