VOOZH about

URL: https://www.geeksforgeeks.org/dsa/cses-solutions-string-matching/

⇱ CSES Solutions - String Matching - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

CSES Solutions - String Matching

Last Updated : 23 Jul, 2025

Given a string S and a pattern P, your task is to count the number of positions where the pattern occurs in the string.

Examples:

Input: S = "saippuakauppias", P = "pp"
Output: 2
Explanation: "pp" appears 2 times in S.

Input: S = "aaaa", P = "aa"
Output: 3
Explanation: "aa" appears 3 times in S.

Approach: To solve the problem, follow the below idea:

To find all occurrences of a pattern in a text we can use various String-Matching algorithms. The Knuth-Morris-Pratt (KMP) algorithm is a suitable choice for this problem. KMP is an efficient string-matching algorithm that can find all occurrences of a pattern in a string in linear time.

Concatenate the Pattern and Text: The first step is to concatenate the pattern and the text with a special character # in between. This is done to ensure that the pattern and text don’t overlap during the computation of the prefix function.

Compute the Prefix Function: The computePrefix function is used to compute the prefix function of the concatenated string. The prefix function for a position i in the string is defined as the maximum proper prefix of the substring ending at position i that is also a suffix of this substring. This function is a key part of the KMP algorithm.

Count the Occurrences: After the prefix function is computed, the next step is to count the number of occurrences of the pattern in the text. This is done by iterating over the prefix function array and checking how many times the pattern length appears in the array. Each time the pattern length appears in the array, it means an occurrence of the pattern has been found in the text.

Step-by-step algorithm:

  • Declare the prefix function array pi[], and the count of occurrences.
  • The prefix function is computed for the pattern string. This function calculates the longest proper prefix which is also a suffix for each substring of the pattern. This information is stored in the pi array.
  • The pattern string is concatenated with the text string, with a special character (#) in between to separate them.
  • Iterate over the concatenated string. For each character, check if it matches the current character of the pattern (using the pi[] array). If it does, move to the next character of both the pattern and the text. If it doesn’t, move to the next character of the text, but stay on the current character of the pattern (or move to the character indicated by the pi array).
  • Each time the end of the pattern is reached (i.e., all characters of the pattern have matched), increment the count of occurrences.
  • After the entire text has been scanned, print the count of occurrences.

Below is the implementation of the algorithm:


Output
2

Time Complexity: O(N+M) where N is the length of the text and M is the length of the pattern to be found.
Auxiliary Space: O(N)

Comment
Article Tags:
Article Tags: