Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python的hackerrank中的重复字符串问题?_Python_Python 3.x_String - Fatal编程技术网

使用python的hackerrank中的重复字符串问题?

使用python的hackerrank中的重复字符串问题?,python,python-3.x,string,Python,Python 3.x,String,我们想要找到给定字符串中的“a”的数量乘以无限次。 我们将得到一个数字n,它是无限字符串的切片大小。 样本输入 阿巴 十, 输出:-7 这里aba乘以10,得到“ABAA” a的数量是7 这是我的密码 def repeatedString(s, n): count = 0 inters = s * n reals = s[0:n+1] for i in reals: if (i == 'a'): count += 1

我们想要找到给定字符串中的“a”的数量乘以无限次。 我们将得到一个数字n,它是无限字符串的切片大小。 样本输入 阿巴 十,

输出:-7 这里aba乘以10,得到“ABAA” a的数量是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
我得到2而不是7作为输出(测试用例'aba'10)。我哪里出错了?我只是将给定的字符串乘以n,因为它永远不会大于切片大小

这是问题的链接
没有理由对字符串进行切片

def repeatedString(s, n):
    count = 0
    for index, i in enumerate(s*n):
        if index >= n:
            return count
        if(i == 'a'):
            count += 1

使用python3的解决方案更简单

s = input().strip()
n = int(input())
print(s[:n%len(s)].count('a')+(s.count('a')*(n//len(s))))
因此,如果字符串包含“a”,则仅返回n。否则,计算字符串s中a的数量,现在使用divmond()函数,我找到了可以添加而不超过n的字符串数量。例如,字符串s是“aba”且n=10,因此我可以完全添加3个“abs”,而字符串的长度不会超过10。现在,添加的字符串中的a数(3*2)。现在剩下的要填充的位置等于divmond()函数的余数(y)。现在将字符串s切到y,并找到其中a的数目,然后将其添加到count中

divmond(10,3)返回(10//3)及其余数

def repeatedString(s, n):
    if len(s)==1 and s=="a":
        return n
    count=s.count("a") 
    x,y=divmod(n,len(s))
    count=count*x
    str=s[:y]
    return count+str.count("a")

Python 3中的解决方案:

def repeatedString(s,n):
    i = 0
    c = 0
    for i in s:
        if i == 'a':
            c += 1

    q = int(n / len(s)) #Finding the quotient 
    r = int(n % len(s)) #Finding the remainder
    if r == 0: 
        c *= q 

    else:
        x = 0
        for i in range(r):
            if s[i] == 'a':
                x += 1
        c = c*q + x

    return int(c)

s = input()
n = int(input())
print(repeatedString(s,n))

我使用了一种简单的酉方法。 一次重复中的“a”的数量是
cnt_a
,因此第一个
n
字符中的“a”的数量将是
(cnt_a/len))*n

def repeatedString(s, n):
    if len(s)==1 and s=='a':
        return n
    cnt_a=0
    for i in s:
        if i == 'a':
            cnt_a+=1
    if cnt_a % 2 == 0:
        no_a = (cnt_a/len(s)) * n
        return math.ceil(no_a)
    else:
        no_a = (cnt_a/len(s)) * n
        return math.floor(no_a)

如果给定的字符串模式中存在字符“a”,则获取其重复计数的速度相当快,之后根据所提到的最终字符串的总长度,将尝试重复给定模式相同的次数&因此将重复计数乘以字符串模式将重复的次数。重要的是,如果最终的字符串输入是奇数,那么我们需要识别这些奇数模式,并分别计算字符“a”在奇数字符串模式中的出现次数。最后,对总计数(偶数和奇数)求和将给出预期结果
def repeatedString(s, n):
    # Get the length of input string
    strlen = len(s)
    a_repeat = 0
    # Get the total count of a repeated character from the input string
    for i in range(0,strlen):
        if s[i] == 'a':
            a_repeat = a_repeat + 1
    # Get the multiplier to make sure that desired input string length achieved
    str_multiplier = int(n // strlen) 
    # Get the repeated count if new string is been created
    result = a_repeat*str_multiplier
    new_str = s[:int( n % strlen )]
    # for odd length of string, get the remaining characters and find repated characters count and add up it to final count
    for i in range(0, len(new_str)):
        if new_str[i] == 'a':
            result += 1
    return result

如果你想要一个更具可读性的答案

def repeatedString(s, n):
    target = 'a'
    target_count = 0

    # how many times does the string need to be repeated: (n // len(s) * s) + s[:(n % len(s))] 
    quotient = n // len(s)
    remainder = n % len(s)

    for char in s:  # how many times target appears in 1 instance of the substring
        if char == target:
            target_count += 1
        
    # how many times the target appears in many instances of the substring provided
    target_count = target_count * quotient

    for char in s[:remainder]:  # count the remaining targets in the truncated substring
        if char == target:
            target_count += 1

    return target_count

添加您自己对问题的描述,回答您的人不应该仅仅为了理解您的问题而导航。@A.Abramov您可以用
count()
method@MuhsinMuhammed固定的。请你测试一下好吗?@komatiraju032我很清楚,但由于这家伙提到他使用的是黑客等级,所以在解决这个问题时最好还是坚持自己的算法,所以他遵循的方法学,因为这家伙提到他使用的是黑客等级,在解决这个问题时,最好坚持他自己的算法。在Hackerrank中回答这个问题:-return(s[:n%len(s)]。count('a')+(s.count('a')*(n//len(s)))@MuhsinMuhammed这正是我在评论中的意思。测试我的解决方案,我想你会发现它更容易执行。@MuhsinMuhammed:你不需要将字符串乘以(这会使计数永远花费时间),而是将一个字符串实例的计数乘以切片中字符串的完整实例数,然后在最后数一数不完整的部分。@voidpro您能解释一下您是如何想出这个python方法来解决这个问题的吗?我也试过同样的问题,我自己也想出了一个相当不错的算法解决方案。虽然这段代码可能会解决这个问题,但如何以及为什么解决这个问题会真正有助于提高你的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。虽然这段代码可以解决这个问题,但如何以及为什么解决这个问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请您的回答添加解释,并说明适用的限制和假设。添加说明以供参考@PanagiotisSimakis