Issue
We want to find the number of 'a's in a given string s
multiplied infinite times.
We will be given a number n
that is the slicing size of the infinite string.
sample input:
aba 10
output:
7
Here aba
is multiplied with 10, resulting 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 instead of 7 as the output (test case 'aba' 10). Where did I go wrong? I just multiplied the given string with n
because it will never be greater than the slicing size.
Here's the link to the problem: https://www.hackerrank.com/challenges/repeated-string/problem
Solution
There's no reason to slice the string
def repeatedString(s, n):
count = 0
for index, i in enumerate(s*n):
if index >= n:
return count
if(i == 'a'):
count += 1
# empty string
return count
Answered By - A. Abramov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.