[Python] hackerrank problem find substring in a string [closed]

Discussão em 'Python' iniciado por Stack, Julho 1, 2025.

  1. Stack

    Stack Membro Participativo

    def count_substring(string, sub_string):
    list_char = [string[i:i+3] for i in range(len(string))]
    # I try to put 3 part in a list
    count =0
    for element in list_char:
    if element == sub_string:
    count +=1
    return count

    if __name__ == '__main__':
    string = input().strip()
    sub_string = input().strip()

    count = count_substring(string, sub_string)
    print(count)


    I split the string into 3-element strings and put them in a list but it still doesn't match all the test cases. Can anyone help me improve it?

    Continue reading...

Compartilhe esta Página