The Minion Game

  • + 3 comments

    Nice solution! I prefer a forward traversal (yours is reverse). Here's my version of your code:

    def count_substring(string, sub_string):
        counter = 0
        len_str = len(string)
        len_sub = len(sub_string)
        #looping over a set of numbers starting at 0 through the length of string exclusive
        for i in range(0, len_str):
            #if string's contents ranging from i through i plus the length of the sub_string
            #is equal to the sub_string
            if string[i:i+len_sub] == sub_string:
                #then increment the counter
                counter += 1
        return counter