Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active September 30, 2020 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbvf50mobile/5968df7a8c5e7463f16480a5dc0b2864 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/5968df7a8c5e7463f16480a5dc0b2864 to your computer and use it in GitHub Desktop.
Just PHP FUN 116.
<?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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment