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.

Example
s="abcac"
n=10
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
Returns
  • 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.

Constraints
1≤∣s∣≤10
1≤n≤10^12
For 25 % of the test cases,n≤10^6 .

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

Sample Input 1
a
1000000000000
Sample Output 1
1000000000000
Explanation 1
Because all of the first n=1000000000000. letters of the infinite string are a, we return 1000000000000.
Output :
Using Python 3 -
				
					#!/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):
    # 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')

    fptr.close()

				
			
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