1)Find the number of occurrence of sub-string in main string.
Input Format
The first line of input contains the original string. The next line contains the substring.
Output Format
Output the integer number indicating the total number of occurrences of the substring in the original string.
Approach
Slicing the main string to substring's length and checking if (sliced part)is equal to substring.
Solution
def count_substring(string, sub_string):
l1=len(string)
l2=len(sub_string)
count=0
for i in range(0,l1):
if(string[i:i+l2]==sub_string):
count+=1
print(count)
string = input().strip()
sub_string = input().strip()
count_substring(string, sub_string)
Sprb ka
ReplyDelete