VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-of-distinct-substrings-occurring-consecutively-in-a-given-string/

⇱ Count of Distinct Substrings occurring consecutively in a given String - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count of Distinct Substrings occurring consecutively in a given String

Last Updated : 15 Jul, 2025

Given a string str, the task is to find the number of distinct substrings that are placed consecutively in the given string.

Examples:

Input: str = "geeksgeeksforgeeks" 
Output:
Explanation:
geeksgeeksforgeeks -> {"geeks"} 
geeksgeeksforgeeks -> {"e"} 
Only one consecutive occurrence of "e" is considered. 
Therefore two distinct substrings {"geeks", "e"} occur consecutively in the string. 
Therefore, the answer is 2.

Input: s = "geeksforgeeks" 
Output:
Explanation:
geeksgeeksforgeeks -> {"e", "e"} 
Only one substring {"e"} occurs consecutively in the string. 

Naive Approach:
The simplest approach is to generate all possible substrings of the given string, and for each substring, find the count of substrings in the given occurring consecutively in the string. Finally, print the count.

Time Complexity: O(N3)
Auxiliary Space: O(N)

Efficient Approach: 
To optimize the above approach, the idea is to use Dynamic Programming
Follow the steps below to solve the problem:

  1. If the length of the string does not exceed 1, then it is not possible to find any such consecutively placed similar substrings. So return 0 as the count.
  2. Otherwise, initialize a memoization table dp[] of dimensions (N+1 * N+1) which is initialized to 0.
  3. Initialize an unordered_set to store the distinct substrings placed consecutively.
  4. Iterate from the end of the string.
  5. While traversing the string if any repeating character is found, then dp[i][j] will be determined considering the previously computed dp value i.e., count of identical substrings up to dp[i+1][j+1] characters and including the current character.
  6. If the character is not similar then, dp[i][j] will be filled with 0.
  7. Similar substrings are consecutively placed together without any other characters and they will be the same for at most (j - i) characters. Hence, for valid substrings, dp[i][j] value must be greater than (j - i). Store those substrings in unordered_set which appears the maximum number of times consecutively.
  8. Finally, return the size of the unordered_set as the count of distinct substrings placed consecutively.

Below is the implementation of the above approach:


Output
2

Time Complexity: O(N^2) 
Auxiliary Space: O(N^2)

Efficient approach: Space optimization

In previous approach the dp[i][j] is depend upon the current and previous row of 2D matrix. So to optimize space we use two vectors curr and prev that keep track of current and previous row of DP.

Implementation Steps:

  • Initialize a vectors prev of size N+1 to keep track of only previous row of Dp with 0.
  • Now iterative over subproblems and get the current computation.
  • While Initialize a vectors curr of size N+1 to keep track of only current row of Dp with 0.
  • Now compute the current value by the help of prev vector and store that value in curr.
  • After every iteration store values of curr vector in prev vector for further iterration.
  • At last create substring and return its size.

Implementation:


Output
2

Time Complexity: O(N^2) 
Auxiliary Space: O(N)

Comment