Occurrences of Sub in Parent
Coding Problem Keys
Occurrences of Sub in Parent
Problem Statement
You are given two strings containing only English letters. Write an algorithm to count the number of occurrences of the second string in the first string. (You may disregard the case of the letters).
Input
The first line of the input consists of a string parent, representing the first string.
The second line consists of a string sub, representing the second string.
Output
Print an integer representing the number of occurrences of Sub in Parent. If no occurrence of Sub is found in Parent then print 0.
Example 1
Input
TimisplayinginthehouseofTimwiththetoysofTimTim
Output
3
Explanation
Tim occurs 3 times in the first string,
So, the output is 3.
Example 2
Input
abcdefgbcd
Output
1
Explanation
Tim occurs 1 times in the first string,
So, the output is 1.
Solution
Programming Language: Python 3 Language
# Published By PKJCODERS""" """ def countOccur(parent,sub): # Write your code here parent = parent.lower() sub = sub.lower() numOfOccurrences = 0 parentLen = len(parent) subLen = len(sub) for i in range(parentLen): if parent[i:i+subLen] == sub: numOfOccurrences +=1 return numOfOccurrences def main(): # input for parent parent = str(input()) # input for sub sub = str(input()) result = countOccur(parent,sub) print(result) if __name__ == "__main__": main()
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments