We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This code works pretty well even though it can be written a lot shorter. I'll help explain since he never did. The first two if statements are trying to avoid really long series if it just has n = 100000000 and s=a. Not needed if you coded in a better way, but it works. Now starting with the "l" variable. That counts the number of "a's" in a given series, so [a,b,c,a] would return 2. math.floor part makes it so it divides by the integral and leaves the remainder out. I recommend just doing n//len(s) though. It's faster. l*c counts all the a's for the integral side, but now you have to take into account the remainder side. That is what res += .... does. the n%len(s) gives the remainder side. Puting it in a position of s[:x] makes it count only the list up to the remainder number. Then s[].count('a') counts the a's in that remainer. The res += just adds the a's from the remainder side to the integral side that you did in the line above.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Repeated String
You are viewing a single comment's thread. Return to all comments →
This code works pretty well even though it can be written a lot shorter. I'll help explain since he never did. The first two if statements are trying to avoid really long series if it just has n = 100000000 and s=a. Not needed if you coded in a better way, but it works. Now starting with the "l" variable. That counts the number of "a's" in a given series, so [a,b,c,a] would return 2. math.floor part makes it so it divides by the integral and leaves the remainder out. I recommend just doing n//len(s) though. It's faster. l*c counts all the a's for the integral side, but now you have to take into account the remainder side. That is what res += .... does. the n%len(s) gives the remainder side. Puting it in a position of s[:x] makes it count only the list up to the remainder number. Then s[].count('a') counts the a's in that remainer. The res += just adds the a's from the remainder side to the integral side that you did in the line above.