I have this challenge: Repeated String to solve. I have been trying to solve the challenge but getting failure cos of Memory Failure. I cannot help this situation cos the HackerRank platform is not supporting my solution. Might be 32-bit platform.
I have this solution for this, which is quite working for problem having smaller length, but I have worked on this thing to learn according to less memory usage.
My Code:
def repeatedString(s, n):
if(s != 'a'):
return len([x for x in (s*n)[:n] if x == 'a'])
return n
Now this throws Memory Error
error for input having very large length, and string.
I have researched on it, and saw some submissions, and found this out.
Correct Solution from Leaderboard:
def repeatedString(s, n):
L = len(s)
return (s.count('a') * (n//L) + s[:n % L].count('a'))
That's it! I got so confused by this solution that I could figure what is actually happening and how. Could anybody please let me know how the above correct solution works? I am new to python and trying my best to get my hands dirty on competitive coding. Thank You!