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.
Let's take the example of
s="aba" and n=10.
The length of "aba" can fit into 10 three times, and "a" occurs twice in the string so 2x3 = 6. However, this only accounts for the first 9 numbers, and we need 10, which means we need to get the remainder. The modulo operation allows us to save that remainder in mod. So
s[:mod].count('a') in this case would be s[:1].count('a')
which calculates to 1, since there is only one 'a' in a range from s[0] to s[1]
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 →
Let's take the example of s="aba" and n=10.
The length of "aba" can fit into 10 three times, and "a" occurs twice in the string so 2x3 = 6. However, this only accounts for the first 9 numbers, and we need 10, which means we need to get the remainder. The modulo operation allows us to save that remainder in mod. So s[:mod].count('a') in this case would be s[:1].count('a') which calculates to 1, since there is only one 'a' in a range from s[0] to s[1]