Skip to content

Instantly share code, notes, and snippets.

@MagedAhmad
Created April 15, 2020 15:24
Show Gist options
  • Select an option

  • Save MagedAhmad/6e7a64e0fc0cec45cbd713f8036b0cbc to your computer and use it in GitHub Desktop.

Select an option

Save MagedAhmad/6e7a64e0fc0cec45cbd713f8036b0cbc to your computer and use it in GitHub Desktop.
Hackerrank repeated String
<?php
// Complete the repeatedString function below.
function repeatedString($s, $n) {
$i = 0;
$count = 0;
while($i < strlen($s)){
if ($s[$i] == 'a') {
$count++;
}
$i++;
}
$total = floor($n / strlen($s));
$total *= $count;
if ($n%strlen($s)) {
$r = $n % strlen($s);
for ($i = 0; $i < $r; $i++) if ($s[$i] == 'a') $total++;
}
return $total;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment