Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (12.7k points)

We need to track down the number of 'a's in a given string s increased boundless occasions. we will be given a number n that is the cutting size of the endless string. test input aba 10 

Output:- 7 here aba is increased(multiplied) with 10 coming about in 'abaabaabaa' and the no.of 'a's are 7 this is my code 

def repeatedString(s, n):

    count = 0

    inters = s * n

    reals = s[0:n+1]

    for i in reals:

        if (i == 'a'):

            count += 1

    return count

I'm getting 2 rather than 7 as the yield (testcase 'aba' 10). where did I turn out badly? I just duplicated the given string with n since it won't ever be more noteworthy than the cutting size.

Here's the link for the problem: https://www.hackerrank.com/challenges/repeated-string/problem

1 Answer

0 votes
by (26.4k points)

Look at the below code using python3

s = input().strip()

n = int(input())

print(s[:n%len(s)].count('a')+(s.count('a')*(n//len(s))))

Are you pretty much interested to learn python in detail? Come and join the python training course to gain more knowledge.

Browse Categories

...