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:2Explanation: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:1Explanation:

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(N^{3})**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:

- 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**. - Otherwise, initialize a memoization table
**dp[]**of dimensions**(N+1 * N+1)**which is initialized to**0**. - Initialize an
**unordered_set**to store the distinct substrings placed consecutively. - Iterate from the end of the string.
- 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. - If the character is not similar then,
**dp[i][j]**will be filled with 0. - 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. - Finally, return the size of the
**unordered_set**as the count of distinct substrings placed consecutively.

Below is the implementation of the above approach:

## C++

`// C++ Program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to count the distinct substrings` `// placed consecutively in the given string` `int` `distinctSimilarSubstrings(string str)` `{` ` ` `// Length of the string` ` ` `int` `n = str.size();` ` ` `// If length of the string` ` ` `// does not exceed 1` ` ` `if` `(n <= 1) {` ` ` `return` `0;` ` ` `}` ` ` `// Initialize a DP-table` ` ` `vector<vector<` `int` `> > dp(` ` ` `n + 1, vector<` `int` `>(n + 1, 0));` ` ` `// Stores the distinct substring` ` ` `unordered_set<string> substrings;` ` ` `// Iterate from end of the string` ` ` `for` `(` `int` `j = n - 1; j >= 0; j--) {` ` ` `// Iterate backward until` ` ` `// dp table is all computed` ` ` `for` `(` `int` `i = j - 1; i >= 0; i--) {` ` ` `// If character at i-th index is` ` ` `// same as character at j-th index` ` ` `if` `(str[i] == str[j]) {` ` ` `// Update dp[i][j] based on` ` ` `// previously computed value` ` ` `dp[i][j] = dp[i + 1][j + 1] + 1;` ` ` `}` ` ` `// Otherwise` ` ` `else` `{` ` ` `dp[i][j] = 0;` ` ` `}` ` ` `// Condition for consecutively` ` ` `// placed similar substring` ` ` `if` `(dp[i][j] >= j - i) {` ` ` `substrings.insert(` ` ` `str.substr(i, j - i));` ` ` `}` ` ` `}` ` ` `}` ` ` `// Return the count` ` ` `return` `substrings.size();` `}` `// Driver Code` `int` `main()` `{` ` ` `string str = ` `"geeksgeeksforgeeks"` `;` ` ` `cout << distinctSimilarSubstrings(str);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above approach ` `import` `java.io.*;` `import` `java.util.ArrayList;` `class` `GFG{` `// Function to count the distinct substrings ` `// placed consecutively in the given string ` `static` `int` `distinctSimilarSubstrings(String str)` `{` ` ` ` ` `// Length of the string ` ` ` `int` `n = str.length();` ` ` ` ` `// If length of the string ` ` ` `// does not exceed 1 ` ` ` `if` `(n <= ` `1` `)` ` ` `return` `0` `;` ` ` ` ` `// Initialize a DP-table ` ` ` `long` `dp[][] = ` `new` `long` `[n + ` `1` `][n + ` `1` `];` ` ` ` ` `// Declaring ArrayList to store strings` ` ` `ArrayList<String> list = ` `new` `ArrayList<String>();` ` ` `// Iterate from end of the string ` ` ` `for` `(` `int` `j = n - ` `1` `; j >= ` `0` `; j--) ` ` ` `{` ` ` ` ` `// Iterate backward until` ` ` `// dp table is all computed` ` ` `for` `(` `int` `i = j - ` `1` `; i >= ` `0` `; i--) ` ` ` `{` ` ` ` ` `// If character at i-th index is` ` ` `// same as character at j-th index` ` ` `if` `(str.charAt(i) == str.charAt(j)) ` ` ` `{` ` ` ` ` `// Update dp[i][j] based on` ` ` `// previously computed value` ` ` `dp[i][j] = dp[i + ` `1` `][j + ` `1` `] + ` `1` `;` ` ` `}` ` ` ` ` `// Otherwise` ` ` `else` ` ` `{` ` ` `dp[i][j] = ` `0` `;` ` ` `}` ` ` `// Condition for consecutively` ` ` `// placed similar substring` ` ` `if` `(dp[i][j] >= j - i)` ` ` `{` ` ` `list.add(str.substring(j - i, i));` ` ` `}` ` ` `}` ` ` `}` ` ` ` ` `// Return the count` ` ` `return` `list.size();` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `String str = ` `"geeksforgeeks"` `;` ` ` ` ` `System.out.println(distinctSimilarSubstrings(str));` `}` `}` `// This code is contributed by user_00` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement ` `# the above approach ` `# Function to count the distinct substrings` `# placed consecutively in the given string` `def` `distinctSimilarSubstrings(` `str` `):` ` ` `# Length of the string` ` ` `n ` `=` `len` `(` `str` `)` ` ` `# If length of the string` ` ` `# does not exceed 1` ` ` `if` `(n <` `=` `1` `):` ` ` `return` `0` ` ` `# Initialize a DP-table` ` ` `dp ` `=` `[[` `0` `for` `x ` `in` `range` `(n ` `+` `1` `)]` ` ` `for` `y ` `in` `range` `(n ` `+` `1` `)]` ` ` `# Stores the distinct substring` ` ` `substrings ` `=` `set` `()` ` ` `# Iterate from end of the string` ` ` `for` `j ` `in` `range` `(n ` `-` `1` `, ` `-` `1` `, ` `-` `1` `):` ` ` `# Iterate backward until` ` ` `# dp table is all computed` ` ` `for` `i ` `in` `range` `(j ` `-` `1` `, ` `-` `1` `, ` `-` `1` `):` ` ` `# If character at i-th index is` ` ` `# same as character at j-th index` ` ` `if` `(` `str` `[i] ` `=` `=` `str` `[j]):` ` ` `# Update dp[i][j] based on` ` ` `# previously computed value` ` ` `dp[i][j] ` `=` `dp[i ` `+` `1` `][j ` `+` `1` `] ` `+` `1` ` ` `# Otherwise` ` ` `else` `:` ` ` `dp[i][j] ` `=` `0` ` ` `# Condition for consecutively` ` ` `# placed similar substring` ` ` `if` `(dp[i][j] >` `=` `j ` `-` `i):` ` ` `substrings.add(` `str` `[i : j ` `-` `i])` ` ` `# Return the count ` ` ` `return` `len` `(substrings)` `# Driver Code` `str` `=` `"geeksgeeksforgeeks"` `# Function call` `print` `(distinctSimilarSubstrings(` `str` `))` `# This code is contributed by Shivam Singh` |

*chevron_right*

*filter_none*

**Output:**

2

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

## Recommended Posts:

- Count the number of vowels occurring in all the substrings of given string
- Count substrings with each character occurring at most k times
- Find distinct characters in distinct substrings of a string
- Element which occurs consecutively in a given subarray more than or equal to K times
- Maximum sum from three arrays such that picking elements consecutively from same is not allowed
- Maximize number of days for which P chocolates can be distributed consecutively to N people
- Count of distinct substrings of a string using Suffix Trie
- Count of distinct substrings of a string using Suffix Array
- Count distinct substrings of a string using Rabin Karp algorithm
- Count number of distinct substrings of a given length
- Count number of substrings with exactly k distinct characters
- Count distinct substrings that contain some characters at most k times
- Count of substrings of length K with exactly K distinct characters
- Count of Substrings with at least K pairwise Distinct Characters having same Frequency
- Count of substrings having all distinct characters
- Generate a String of having N*N distinct non-palindromic Substrings
- Minimum changes to a string to make all substrings distinct
- Frequency of maximum occurring subsequence in given string
- Count ways to split a Binary String into three substrings having equal count of zeros
- Count of maximum occurring subsequence using only those characters whose indices are in GP

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.