Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active August 4, 2020 17:25
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/37cc4e717ead9d872c7a93bcb91ce5d5 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/37cc4e717ead9d872c7a93bcb91ce5d5 to your computer and use it in GitHub Desktop.
Just PHP FUN 067.
<?php
# https://www.codewars.com/kata/56a5d994ac971f1ac500003e Consecutive strings.
function longestConsec($strarr, $k) {
$n = count($strarr);
if(0 == $n || $k <= 0 || $k > $n) return "";
// Concatinate frist $k strings
$x = array_sum(array_map('strlen',array_slice($strarr,0,$k)));
$max = $x;
$start = 0;
for($i = 1; $i <= $n - $k; $i += 1){
$x = $x - strlen($strarr[$i-1]) + strlen($strarr[$i + $k - 1]);
if($x > $max){ $max = $x; $start = $i;}
}
return implode("",array_slice($strarr,$start,$k));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment