• + 0 comments

    My solution using Python.

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    #
    # Complete the 'repeatedString' function below.
    #
    # The function is expected to return a LONG_INTEGER.
    # The function accepts following parameters:
    #  1. STRING s
    #  2. LONG_INTEGER n
    #
    
    def repeatedString(s, n):
        # Write your code here
        count_a_in_s = s.count('a')
        
        full_repeats = n // len(s)
        
        total_a_in_full_repeats = full_repeats * count_a_in_s
        
        remainder = n % len(s)
        
        total_a_in_remainder = 0
        for i in range(remainder):
            if s[i] == 'a':
                total_a_in_remainder += 1
        
        total_a = total_a_in_full_repeats + total_a_in_remainder
        
        return total_a
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        s = input()
    
        n = int(input().strip())
    
        result = repeatedString(s, n)
    
        fptr.write(str(result) + '\n')
    
        fptr.close()