Write Function Python Counts Many Non Overlapping Occurences Substring Appear String Tried Q37154960

Write a function with Python that counts how manynon-overlapping occurences of a substring appear in a string.

I tried but it fails all tests except for one:

def count(substr,theStr):

count = start = 0
while True:
start = substr.find(theStr, start) + 1
if start < 0:
count+=1
else:
return count


Solution


def countSubStr(substr,theStr): count = 0 while True: start = theStr.find(substr) + 1 theStr = theStr[start+len(substr):] if start > 0: count+=1

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.