VOOZH about

URL: https://www.geeksforgeeks.org/dsa/cses-solution-counting-patterns/

⇱ CSES solution - Counting Patterns - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

CSES solution - Counting Patterns

Last Updated : 23 Jul, 2025

Given a string S and patterns[], count for each pattern the number of positions where it appears in the string.

Examples:

Input: S = "aybabtu", patterns[] = {"bab", "abc", "a"}
Output:
1
0
2
Explanation:

  • "bab" occurs only 1 time in "aybabtu", that is from S[2...4].
  • "bab" does not occur in "aybabtu".
  • "a" occurs only 2 times in "aybabtu", that is from S[0...0] and from S[3...3].

Input: S = "geeksforgeeks", patterns[] = {"geeks", "for", "gfg"}
Output:
2
1
0
Explanation:

  • "geeks" occurs 2 times in "geeksforgeeks", that is from S[0...4] and S[8...12].
  • "for" occurs 1 time in "geeksforgeeks", that is from S[5...7].
  • "gfg" does not occur in "geeksforgeeks".

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

The idea is to uses Suffix Array data structure. A Suffix Array is a sorted array of all suffixes of a given string.

Let's look at the intuition in step-by-steps:

Suffix Array Construction: The first part of the solution is to building a suffix array for the given string. A suffix array is a sorted array of all suffixes of a given string. The buildSuffixArray() function constructs this array. It starts by initializing the suffix array and position array. The position array holds the rank (i.e., lexicographic order) of each suffix. Then, it iteratively sorts the suffixes based on their current and next gap’s characters until all ranks are unique.

Pattern Checking: The checkPattern() function checks if a pattern is present at a given position in the suffix array. It compares the characters of the pattern with the characters of the suffix starting at the given position. If the pattern is lexicographically smaller, it returns -1; if larger, it returns 1; if equal and the pattern length is less than or equal to the remaining length of the suffix, it returns 0.

Pattern Searching: Our solve() function performs a binary search for the leftmost and rightmost occurrence of the pattern in the suffix array using the checkPattern() function. The difference between the rightmost and leftmost position plus one will gives the number of occurrences of the pattern in the string.

Step-by-step algorithm:

  • Comparison Function (compareSuffixes):
    • Compares two suffixes based on their positions and characters beyond a specified gap.
    • If positions are different, returns true if the first position is smaller.
    • If positions are equal, compares additional characters beyond the gap.
  • Build Suffix Array Function (buildSuffixArray):
    • Initializes the suffix array and positions based on the characters of the input string.
  • Uses a loop to build the suffix array:
    • Sorts the suffix array based on the comparison function.
    • Updates the positions based on the sorted order.
    • Checks if all suffixes are in order; if so, exits the loop.
  • Pattern Check Function (checkPattern):
  • Checks if a pattern is present at a given position in the suffix array.
  • Returns -1 if the pattern is smaller, 0 if it matches, and 1 if it's greater.
  • Pattern Search Function (solve):
    • Uses binary search to find the range where the pattern appears in the suffix array.
    • Initializes left and right boundaries.
    • Finds the leftmost occurrence using binary search and updates the left boundary.
    • Finds the rightmost occurrence using binary search and updates the right boundary.
    • Calculates and prints the count of occurrences.

Below is the implementation of the algorithm:


Output
1
0
2

Time Complexity:

Building Suffix Array: O(n log2n)
Checking each Pattern: O(logn)
Overall Time Complexity:(mlogn + nlog2n), , where m is the number of patterns and n is the length of the input string.

Auxiliary Space Complexity: O(n) due to the arrays suffixArray, position, and temp. These arrays are used to store information about the suffix array and the intermediate steps in its construction.

Comment
Article Tags:
Article Tags: