Coding Challenge: Implement Pattern Count

Use knowledge from the last lesson to perform the following exercise.

We'll cover the following

Pattern frequency

Pattern Count Problem

Problem overview:
Provided with two strings, Text and Pattern, find the number of times Pattern is repeated in Text.


Input: Strings Text and Pattern.
Output: Count(Text, Pattern).

Sample dataset:

GCGCG
GCG

Sample output:

2

Press + to interact
def PatternCount(Text, Pattern):
count = 0
# Write your code here
return count

Solution explanation

  • Line 2: We define the variable count for storing the pattern count.
  • Lines 3–5: We iterate over Text to find the provided Pattern in that text.
    • Line 4: We check if the substring from i to the length of the pattern is equal to Pattern.
    • Line 5: We increment the count if the condition is met.