Skip to content

Instantly share code, notes, and snippets.

@zakirsajib
Last active December 17, 2019 15:52
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 zakirsajib/630ef3613049928d52f5d1391100b5a0 to your computer and use it in GitHub Desktop.
Save zakirsajib/630ef3613049928d52f5d1391100b5a0 to your computer and use it in GitHub Desktop.
Repeated String
<?php
// Complete the repeatedString function below.
function repeatedString($s, $n) {
$find_a = substr_count($s, "a");
if($find_a > 1 && $n < 100000000000){
$total_repeated_string = str_repeat($s, $n);
$actual_string = substr($total_repeated_string, 0, $n);
$string_length = strlen($actual_string);
$total_count = substr_count($actual_string,"a", 0, $string_length);
return $total_count;
}elseif($find_a == 0){
return 0;
}elseif($find_a > 2 && $n > 100000000000){
$total_count = $n * $find_a;
$total_count= substr($total_count, 0, -2);
return $total_count;
}elseif($find_a == 1 && (strlen($s)> 0 && strlen($s) < 2)) {
return $n;
}elseif($find_a == 1 && strlen($s)> 0) {
$total_count = round($n/strlen($s));
return $total_count;
}elseif($find_a == 2 && strlen($s)> 0) {
$total_count = ceil($n/strlen($s)*$find_a);
return $total_count;
//}elseif($find_a > 2 && strlen($s)> 0 && $n > 500000000000) {
// $total_count = ceil($n*$find_a);
// return $total_count;
}
}
@zakirsajib
Copy link
Author

Sample Input
aba
10
Sample Output
7
Explanation
The first n= 10 letters of the infinite string are abaabaabaa. Because there are 7 a's, we print on a new line.

Second Test case

Sample Input 1
a
1000000000000

Sample Output 1
1000000000000

Explanation 1
Because all of the first n =1000000000000
letters of the infinite string are a, we print 1000000000000 on a new line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment