Answered step by step
Verified Expert Solution
Question
1 Approved Answer
how_many_occurrences Write a function to count the number of times a string s2 occurs as a substring of s. For example how_many_occurrences(mississsippi, ss) should return
how_many_occurrences
Write a function to count the number of times a string s2 occurs as a substring of s.
For example how_many_occurrences("mississsippi", "ss") should return 3. You may assume that len(s2) <= len(s) and that s and s2 will be strings of lowercase letters.
Note: Yes, we know "mississippi" is spelled wrong, this is done to show how overlapping occurrences should be handled
Below is some pseudocode that may help with coming up with your solution. You do need to follow this algorithm if you do not want to.
- Initialize a variable count to represent the number of times we see s2 in s
- While s contains s2 do the following
- Use the find() function to get the location of the first character of the first occurrence of s2 in s.
- Remove the prefix up to and including the first character of s2 in s.
- Increment count by 1
- return the value of count
# Complete the function below def how_many_occurrences(s, s2): """ Returns the number of times s2 occurs as a substring of s """ # *** WRITE YOUR CODE HERE *** # how_many_occurrences("mississsippi", "ss") # Correct answer is 3 # NOTE: Yes, we know mississippi is spelled wrong, this is done to show how overlapping occurrences should be handled
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started