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.
numOfS calculates how many times string s has appeared as a complete string, up to nth character in the infinite string .
rest represents the number of characters left after finding out number of s.
For example, let's say we have a string s "abcd" and n is equal to 10. The infinite string will be something like "abcdabcdabcd...".
Then, we can break this infinite string into substrings like this up to 10th character, abcd/abcd/ab.
In this case, we have 2 complete sets of s and a partial s which contains only 2 characters.
numOfS gives us 2.5, but long is a type of integer so the result becomes 2 and 0.5 is gone. The value 2.5 means we have 2 complete sets of s and 0.5, half of s.
Since ab is not a complete s, we need to figure out how to pick up that 0.5 we dropped above.
Now, rest counts what's left, remainders. 10 % 4 is 2. Finally, these results will be passed onto aCounter, which literally counts the number of As in the string, depending on the algorithm.
The rest is pretty easy to understand :)
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 →
numOfS calculates how many times string s has appeared as a complete string, up to nth character in the infinite string . rest represents the number of characters left after finding out number of s.
For example, let's say we have a string s "abcd" and n is equal to 10. The infinite string will be something like "abcdabcdabcd...". Then, we can break this infinite string into substrings like this up to 10th character, abcd/abcd/ab. In this case, we have 2 complete sets of s and a partial s which contains only 2 characters.
numOfS gives us 2.5, but long is a type of integer so the result becomes 2 and 0.5 is gone. The value 2.5 means we have 2 complete sets of s and 0.5, half of s. Since ab is not a complete s, we need to figure out how to pick up that 0.5 we dropped above.
Now, rest counts what's left, remainders. 10 % 4 is 2. Finally, these results will be passed onto aCounter, which literally counts the number of As in the string, depending on the algorithm.
The rest is pretty easy to understand :)