- https://www.codewars.com/kata/5c8bf3ec5048ca2c8e954bf3 Shortest Distance to a Character.
Started at 22:45 30.09.2020 Wednesday Sep.
Finished at 23:03 30.09.2020 Wednesday Sep. (0hr 18minutes)
Started at 22:45 30.09.2020 Wednesday Sep.
Finished at 23:03 30.09.2020 Wednesday Sep. (0hr 18minutes)
<?php | |
# https://www.codewars.com/kata/5c8bf3ec5048ca2c8e954bf3 Shortest Distance to a Character. | |
function shortesttoChar($str,$c){ | |
if("" == $c) return []; | |
$size = strlen($str); $answer = []; | |
$hsh = []; | |
for($i = 0; $i < $size; $i += 1) if($str[$i] == $c) array_push($hsh,$i); | |
if(0 == count($hsh)) return []; | |
for($i = 0; $i < $size; $ +=1){ | |
$min = $size; | |
foreach($hsh as $j) if(abs($j-$i) < $min) $min = abs($j-$i); | |
array_push($answer); | |
} | |
return $answer; | |
} |