Last active
September 10, 2019 07:24
-
-
Save barakpinchovski/547d73e3c905d7aebba515ec838ecd90 to your computer and use it in GitHub Desktop.
PHP solution for https://www.hackerrank.com/challenges/repeated-string/
This file contains 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 | |
$s = 'kmretasscityylpdhuwjirnqimlkcgxubxmsxpypgzxtenweirknjtasxtvxemtwxuarabssvqdnktqadhyktagjxoanknhgilnm'; | |
$n = 736778906400; | |
echo repeatedString($s, $n); | |
function repeatedString($s, $n) { | |
Define('CHAR', 'a'); | |
if (!gettype($n) === 'integer' || !gettype($s) === 'string' || !$s || $n <= 0) { | |
return 0; | |
} | |
if (!strcasecmp($s, CHAR)) { | |
return $n; | |
} | |
$len = strlen($s); | |
$occurrences = substr_count($s, CHAR); | |
$occurrences *= floor($n / $len); | |
$occurrences += substr_count(substr($s, 0, $n % $len), CHAR); | |
return $occurrences; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment