Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

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

closed

4 Answers

0 votes
by (19k points)
selected by
 
Best answer
Here's a answer for the above question:

s = "aba"

n = 10

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

print("The number of 'a's in the repeated string is:", count)

In this shorter version, the count() method is used to count the occurrences of 'a' in the original string s. The count is then multiplied by the number of times s can be repeated fully in the concatenated string s * n. To account for any remaining characters after the repeated string, s[:n % len(s)] is used to take a slice of s up to the remaining length. The count() method is applied again to count the occurrences of 'a' in the remaining slice. The two counts are added together to obtain the final count of 'a's in the repeated string.
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.

0 votes
by (25.7k points)
The issue in your code lies with the line reals = s[0:n+1]. It should be reals = inters[0:n] instead.

The reason for the correction is that inters is the concatenated string obtained by repeating string s n times. So, to count the number of 'a's correctly, you should consider reals as a slice of inters from index 0 to index n-1. By using s[0:n+1], you are not considering the concatenated string inters but rather the original string s.

Here's the corrected code:

def repeatedString(s, n):

    count = 0

    inters = s * n

    reals = inters[0:n]

    for i in reals:

        if i == 'a':

            count += 1

    return count

s = "aba"

n = 10

result = repeatedString(s, n)

print("The number of 'a's in the repeated string is:", result)

With this correction, the code should correctly count the number of 'a's in the repeated string. In the provided example, it will output 7, which is the expected result.
0 votes
by (15.4k points)
def repeatedString(s, n):

    count = 0

    inters = s * n

    reals = inters[:n]

    for char in reals:

        if char == 'a':

            count += 1

    return count

s = "aba"

n = 10

result = repeatedString(s, n)

print("The number of 'a's in the repeated string is:", result)

In this updated version of the code, the function repeatedString takes a string s and a number n as input. It initializes a count variable to keep track of the number of occurrences of 'a'. The string s is repeated n times to obtain inters. Then, a slice of inters from index 0 to index n-1 is taken and stored in reals. The loop iterates over each character in reals, and if the character is 'a', the count is incremented. Finally, the count is returned.

The example input "aba" with n equal to 10 will produce an output of 7, which represents the correct number of 'a's in the repeated string.

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...