• + 0 comments

    Here's a solution in Python that does not need any for loops and can handle even high numbers for n


    def repeatedString(s: str, n: int) -> int:
        s_len = len(s)
        repetitions_needed = n // s_len
        remaining_chars = n % s_len
    
        occurrences_of_a = s.count('a') * repetitions_needed
    
        if remaining_chars:
            occurrences_of_a += s[:remaining_chars].count('a')
    
        return occurrences_of_a