Implement a function that counts the number of times a target string occurs in the main string. In particular, provide the implementation for count(s, target), where 's' is the main string and 'target' is the substring you wish to count. Note that 'target' is not just a single character, but a substring of any length greater than zero (length guaranteed to be positive). Some of the sample cases include, but are not limited to, the following.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 6PE
icon
Related questions
Question
Python Question
QUESTION 1
Recursively count the occurrences of a substring.
Implement a function that counts the number of times a target string occurs in the main string.
In particular, provide the implementation for count(s, target), where 's' is the main string and 'target' is
the substring you wish to count.
Note that 'target' is not just a single character, but a substring of any length greater than zero (length
guaranteed to be positive).
Some of the sample cases include, but are not limited to, the following.
Input: s= 'aaabbb', target = 'aa'
Output: 2
Input: s= 'bbaaa', target = 'c'
Output: 0
Input: s= 'abcbc', target='bcb'
Output: 1
Input: s = 'aab', target='bbbb'
Output: 0
Here are the constraints:
• You mustn't use any of the string methods provided by Python. (Indexing is okay)
• You must use recursion.
• Write a paragraph detailing the base and recursive cases. Your answer should also include the
*why* and *how*.
Transcribed Image Text:Python Question QUESTION 1 Recursively count the occurrences of a substring. Implement a function that counts the number of times a target string occurs in the main string. In particular, provide the implementation for count(s, target), where 's' is the main string and 'target' is the substring you wish to count. Note that 'target' is not just a single character, but a substring of any length greater than zero (length guaranteed to be positive). Some of the sample cases include, but are not limited to, the following. Input: s= 'aaabbb', target = 'aa' Output: 2 Input: s= 'bbaaa', target = 'c' Output: 0 Input: s= 'abcbc', target='bcb' Output: 1 Input: s = 'aab', target='bbbb' Output: 0 Here are the constraints: • You mustn't use any of the string methods provided by Python. (Indexing is okay) • You must use recursion. • Write a paragraph detailing the base and recursive cases. Your answer should also include the *why* and *how*.
Expert Solution
steps

Step by step

Solved in 3 steps with 4 images

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT