Repeated String from HackerRank

Repeated String

Frequency of letter a in repeated string
There is a string ,s, of lowercase English letters that is repeated infinitely many times. Given an integer ,n, find and print the number of letter a's in the first n letters of the infinite string.

The substring we consider is abcacabcac, the first 10 characters of the infinite string. There are 4 occurrences of a in the substring.

Function Description
Complete the repeatedString function in the editor below.
repeatedString has the following parameter(s):
  • s: a string to repeat
  • n: the number of characters to consider
  • int: the frequency of a in the substring
Input Format
The first line contains a single string,s.
The second line contains an integer,n.

For 25 % of the test cases,n≤10^6 .

Sample Input
Sample Input 0
Sample Output 0
Explanation 0 The first letters of the infinite string are abaabaabaa. Because there are 7 a's, we return 7 .

Sample Input 1
Sample Output 1
Explanation 1
Because all of the first n=1000000000000. letters of the infinite string are a, we return 1000000000000.
Output :
Using Python 3 -

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

def repeatedString(s, n):
    # below code is what you need
    r = 0
    l = len(s)
    for i in range(0, l):
        if s[i] == 'a':
            r+= 1
    r*= int(n / l)
    for i in range(0, n % l):
        if s[i] == 'a':
            r+= 1
    return r
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')


Thank You For Visiting Our Website!
This Post is referred by hackerRank you can check the code on hackerRank.
Please Visit : The Minion Game – Code-Tech Community
And make sure to leave us a comment down below , we will be happy to know if this worked for you.
Happy Coding Mates ! 🙂

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Shopping Cart