← Back to challenges

Repeated String

PythonHardmathstrings

Instructions

Edward has a string s of lowercase English letters that he repeated infinitely many times. Given an integer n, find and print the number of letter "a"s in the first n letters of Edward's infinite string. For example, if the string s=abcac and n=10, the substring we consider is abcacabcac, the first 10 characters of his infinite string. There are 4 occurrences of a in the substring.

Complete the repeated_string() method. It should return an integer representing the number of occurrences of a in the prefix of length n in the infinitely repeating string.

Examples

repeated_string("aba", 10) ➞ 7

repeated_string("a", 1000000000000) ➞ 1000000000000

repeated_string("aab",882787) ➞ 588525

Notes

N/A

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.