Find inside
Inputs
text is 0 to 20 lowercase letters (a or b). part is 1 to 3 lowercase letters (a or b).
Your tasks
- Create a number
countthat starts at 0. - Use an outer loop over every start position in
textwherepartcould still fit. - At each start position, use an inner loop to compare
partwith the characters oftextfrom that position. Add 1 tocountif every character matched. - Complete
countOccurrencesso it returnscount. Return it, do not print it.
Example
With text = "aabaab" and part = "aab", countOccurrences returns 2.
Rules
- Matches may overlap. "aa" appears twice in "aaa".
- Do not use a built-in search or count.
Test data
TEST DATA 1: text is "aabaab", part is "aab"
TEST DATA 2: text is "abab", part is "b"
Practice focus
Nested loops compare every pair and build rows. The slow, simple answer to most problems is two loops. Knowing it is what lets you see how to beat it.