You are viewing a single comment's thread. Return to all comments →
def count_substring(string, sub_string): count = 0 start = 0 sub_len = len(sub_string)
while start <= len(string) - sub_len: pos = string.find(sub_string, start) if pos != -1: count += 1 start = pos + 1 # Move one character forward to handle overlapping else: break return count
Seems like cookies are disabled on this browser, please enable them to open this website
Find a string
You are viewing a single comment's thread. Return to all comments →
Need to consider case where charachter overlaps
def count_substring(string, sub_string): count = 0 start = 0 sub_len = len(sub_string)