Hackerank: Interview Prep Kit: Repeating String.
Hi friends welcome to my channel, today we’ll be solving a problem from Hackerank, which you can locate under Interview Prep Kit, in the subcategory Warm-Up Challenges. Titled, Repeating String, quite easy, but tricky. So without much ado, let’s get to it. Note: I will be leaving the solutions in Javascript, Dart, and PHP.
Problem
There is a string, s, of lowercase English letters that is repeated infinitely many times. Given an integer, n, find and print the number of letter a's in the first n letters of the infinite string.
Example
s = 'abcac'
n = 10The substring we consider is abcacabcac, the first 10 characters of the infinite string. There are occurrences of a in the substring.
Function Description
Complete the repeatedString function in the editor below.repeatedString has the following parameter(s):s: a string to repeatn: the number of characters to consider
Returns
int: the frequency of a in the substring
Input Format
The first line contains a single string, s.
The second line contains an integer, n.
Sample Input 0
aba
10
Sample Output 0
7
Explanation 0
The first n = 10 letters of the infinite string are abaabaabaa. Because there are 7 a's, we return 7
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 return 1000000000000
Solution in Dart.
Solution in Javascript
Solution in PHP
There you go, I’ll always encourage you to go through the problem and understand it, try to solve it yourself, then see my solutions and tell me what you think. You can reach me on LinkedIn and Twitter.