Created
April 15, 2020 15:24
-
-
Save MagedAhmad/6e7a64e0fc0cec45cbd713f8036b0cbc to your computer and use it in GitHub Desktop.
Hackerrank repeated String
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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