我们想在给定的字符串 s 中找到乘以无限次的 'a' 的数量。我们将得到一个数字 n,它是无限字符串的切片大小。样本输入 aba 10
输出:- 7 此处 aba 乘以 10 产生 'abaabaabaa' 并且 'a's 的数量是 7 这是我的代码
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
Run Code Online (Sandbox Code Playgroud)
我得到 2 而不是 7 作为输出(测试用例 'aba' 10)。我哪里做错了?我只是将给定的字符串与 n 相乘,因为它永远不会大于切片大小。
这是问题的链接 https://www.hackerrank.com/challenges/repeated-string/problem