Hackerrank – Repeated String

Hackerrank – Problem Statement

A description of the problem can be found on Hackerrank.

Solution

The length of a given word repeated could be too much to be able to calculate the result before the time limit. We need to simplify our solution.

We have a given string – s. Count only "a" characters in the given string – c
Get the length of the result string (given on second line) – n
Find how many times is s in first n characters. – t = n / length(s)
There is a rest of the characters in n-charactede string – rest = n - t * length(s)
Find how many "a"s are in the rest of the string – r.

The result is total number of "a"s:
c * t + r

I created solution in:

All solutions are also available on my GitHub.

Java

JavaScript

Scala

Ruby

Leave a Reply

Your email address will not be published. Required fields are marked *