Input:ABCDCDC
CDC
Output:2
def count_substring(string, sub_string):
for i in range(0,len(string)):
ans=string.count(sub_string)
return ans
I am getting 1 as output how can I get 2 as output and read the full string
>Solution :
From every index look forward for next n characters if they match, n being the size of substring
def count_substring(string, sub_string):
n = len(sub_string)
ans = 0
for i in range(0,len(string)):
if(i+n > len(string)):#out of range problem
break
ans+=string.count(sub_string,i,i+n)
return ans