HackerRank: Repeated String Challenge
This week, I’m going after HackerRank’s Repeated String challenge. I have to build a function that takes in 2 arguments, a string of lowercase letters that is repeated infinity times and an integer. The function will have to take the string, look at the amount of characters passed in as the second argument to the function, and return the amount of times the letter “a” appears in that substring. For example, if the functions arguments are “abc” and 10, the substring will be “abcabcabca” and the returned value will be 7.
One of the problems I faced when trying to solve this challenge lies in the constraints of this problem. For a quarter of the test cases, the passed-in number would be greater than 1 million. Unfortunately, I missed reading that part of the challenge description, so I encountered a bit of trouble with my first attempt at a solution.
Initially, I thought that all I would have to do was extend the initial string so that its length was longer than the passed-in integer. I created a variable set to an empty string and created a while loop to add the initial string to the new variable until that variable’s length was longer than the passed-in integer. Then that new string was run through a loop to count the number of “a”s were in the string, that number was the returned value. This solution worked for about half of the test cases, but the fringe tests with either numbers higher than a million or extremely long strings would error out. I had to find a different way to get to a more efficient solution.
An easy way to find the number of times a letter appears in a string would be to run the match method on the initial string. That method would take in an argument of a regular expression, which returns an array of all occurrences of that specific letter. Then by chaining the length method, I can get the number of “a”s in the original string. Since that number would be the solution if the string wasn’t duplicated, all I would have to do is multiply that variable by the number of times the total string would need to be duplicated to hit the passed-in integer.
This works when whole turns out to be a whole number, but (if you couldn’t guess from the variable name and the use of Math.floor) another step needs to be added if there’s a remainder on the number. Using the same example from the beginning, if you extend the string “abc” to 10 places, you can fit the full string 3 times with a remainder of one (abc/abc/abc/a).
To find the number of “a”s in the remainder string, I ran the modulo operator to find the remainder of n / s.length. That remainder was used in the substring method to remove all parts of the string beyond that index.
I ran a loop through that substring to check if every part of the string was an “a”. If that block was true, I added one to the solution variable that was already set with the total number of “a”s in the whole parts of the string. Returning that finished solution variable completes a solution that passes every test case!