Count of Distinct Substrings occurring consecutively in a given String
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: 2
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: 1
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:
- 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++
#include <bits/stdc++.h>
using namespace std;
int distinctSimilarSubstrings(string str)
{
int n = str.size();
if (n <= 1) {
return 0;
}
vector<vector< int > > dp(
n + 1, vector< int >(n + 1, 0));
unordered_set<string> substrings;
for ( int j = n - 1; j >= 0; j--) {
for ( int i = j - 1; i >= 0; i--) {
if (str[i] == str[j]) {
dp[i][j] = dp[i + 1][j + 1] + 1;
}
else {
dp[i][j] = 0;
}
if (dp[i][j] >= j - i) {
substrings.insert(
str.substr(i, j - i));
}
}
}
return substrings.size();
}
int main()
{
string str = "geeksgeeksforgeeks" ;
cout << distinctSimilarSubstrings(str);
return 0;
}
|
Java
import java.io.*;
import java.util.ArrayList;
class GFG{
static int distinctSimilarSubstrings(String str)
{
int n = str.length();
if (n <= 1 )
return 0 ;
long dp[][] = new long [n + 1 ][n + 1 ];
ArrayList<String> list = new ArrayList<String>();
for ( int j = n - 1 ; j >= 0 ; j--)
{
for ( int i = j - 1 ; i >= 0 ; i--)
{
if (str.charAt(i) == str.charAt(j))
{
dp[i][j] = dp[i + 1 ][j + 1 ] + 1 ;
}
else
{
dp[i][j] = 0 ;
}
if (dp[i][j] >= j - i)
{
list.add(str.substring(j - i, i));
}
}
}
return list.size();
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
System.out.println(distinctSimilarSubstrings(str));
}
}
|
Python3
def distinctSimilarSubstrings( str ):
n = len ( str )
if (n < = 1 ):
return 0
dp = [[ 0 for x in range (n + 1 )]
for y in range (n + 1 )]
substrings = set ()
for j in range (n - 1 , - 1 , - 1 ):
for i in range (j - 1 , - 1 , - 1 ):
if ( str [i] = = str [j]):
dp[i][j] = dp[i + 1 ][j + 1 ] + 1
else :
dp[i][j] = 0
if (dp[i][j] > = j - i):
substrings.add( str [i : j - i])
return len (substrings)
str = "geeksgeeksforgeeks"
print (distinctSimilarSubstrings( str ))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int distinctSimilarSubstrings( string str)
{
int n = str.Length;
if (n <= 1)
return 0;
long [,] dp = new long [n + 1, n + 1];
List< string > list = new List< string >();
for ( int j = n - 1; j >= 0; j--)
{
for ( int i = j - 1; i >= 0; i--)
{
if (str[i] == str[j])
{
dp[i, j] = dp[i + 1, j + 1] + 1;
}
else
{
dp[i, j] = 0;
}
if (dp[i, j] >= j - i)
{
list.Add(str.Substring(i, j - i));
}
}
}
return list.Count;
}
static void Main()
{
string str = "geeksforgeeks" ;
Console.WriteLine(distinctSimilarSubstrings(str));
}
}
|
Javascript
<script>
function distinctSimilarSubstrings(str)
{
var n = str.length;
if (n <= 1) {
return 0;
}
var dp = Array.from(Array(n+1), ()=>Array(n+1).fill(0));
var substrings = new Set();
for ( var j = n - 1; j >= 0; j--) {
for ( var i = j - 1; i >= 0; i--) {
if (str[i] == str[j]) {
dp[i][j] = dp[i + 1][j + 1] + 1;
}
else {
dp[i][j] = 0;
}
if (dp[i][j] >= j - i) {
substrings.add(str.substring(i, j));
}
}
}
return substrings.size;
}
var str = "geeksgeeksforgeeks" ;
document.write( distinctSimilarSubstrings(str));
</script>
|
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:
C++
#include <bits/stdc++.h>
using namespace std;
int distinctSimilarSubstrings(string str)
{
int n = str.size();
if (n <= 1) {
return 0;
}
vector< int > prev(n + 1, 0);
unordered_map<string, int > substrings;
for ( int j = n - 1; j >= 0; j--) {
vector< int > cur(n + 1, 0);
for ( int i = j - 1; i >= 0; i--) {
if (str[i] == str[j]) {
cur[i] = prev[i + 1] + 1;
}
else {
cur[i] = 0;
}
if (cur[i] >= j - i) {
substrings[str.substr(i, j - i)]++;
}
}
prev = cur;
}
return substrings.size();
}
int main()
{
string str = "geeksgeeksforgeeks" ;
cout << distinctSimilarSubstrings(str);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int distinctSimilarSubstrings(String str)
{
int n = str.length();
if (n <= 1 ) {
return 0 ;
}
int [] prev = new int [n + 1 ];
HashMap<String, Integer> substrings = new HashMap<>();
for ( int j = n - 1 ; j >= 0 ; j--) {
int [] cur = new int [n + 1 ];
for ( int i = j - 1 ; i >= 0 ; i--) {
if (str.charAt(i) == str.charAt(j)) {
cur[i] = prev[i + 1 ] + 1 ;
}
else {
cur[i] = 0 ;
}
if (cur[i] >= j - i) {
substrings.merge(str.substring(i, j), 1 , Integer::sum);
}
}
prev = cur;
}
return substrings.size();
}
public static void main(String[] args) {
String str = "geeksgeeksforgeeks" ;
System.out.println(distinctSimilarSubstrings(str));
}
}
|
Python3
def distinctSimilarSubstrings(s):
n = len (s)
if n < = 1 :
return 0
prev = [ 0 ] * (n + 1 )
substrings = {}
for j in range (n - 1 , - 1 , - 1 ):
cur = [ 0 ] * (n + 1 )
for i in range (j - 1 , - 1 , - 1 ):
if s[i] = = s[j]:
cur[i] = prev[i + 1 ] + 1
else :
cur[i] = 0
if cur[i] > = j - i:
substrings[s[i:i + j - i]] = substrings.get(s[i:i + j - i], 0 ) + 1
prev = cur
return len (substrings)
if __name__ = = "__main__" :
str = "geeksgeeksforgeeks"
print (distinctSimilarSubstrings( str ))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int DistinctSimilarSubstrings( string str)
{
int n = str.Length;
if (n <= 1)
{
return 0;
}
int [] prev = new int [n + 1];
Dictionary< string , int > substrings = new Dictionary< string , int >();
for ( int j = n - 1; j >= 0; j--)
{
int [] cur = new int [n + 1];
for ( int i = j - 1; i >= 0; i--)
{
if (str[i] == str[j])
{
cur[i] = prev[i + 1] + 1;
}
else
{
cur[i] = 0;
}
if (cur[i] >= j - i)
{
substrings[str.Substring(i, j - i)] = substrings.GetValueOrDefault(str.Substring(i, j - i), 0) + 1;
}
}
prev = cur;
}
return substrings.Count;
}
public static void Main()
{
string str = "geeksgeeksforgeeks" ;
Console.WriteLine(DistinctSimilarSubstrings(str));
}
}
|
Javascript
function distinctSimilarSubstrings(str) {
let n = str.length;
if (n <= 1) {
return 0;
}
let prev = new Array(n + 1).fill(0);
let substrings = {};
for (let j = n - 1; j >= 0; j--) {
let cur = new Array(n + 1).fill(0);
for (let i = j - 1; i >= 0; i--) {
if (str[i] == str[j]) {
cur[i] = prev[i + 1] + 1;
}
else {
cur[i] = 0;
}
if (cur[i] >= j - i) {
substrings[str.substr(i, j - i)]++;
}
}
prev = cur;
}
return Object.keys(substrings).length;
}
let str = "geeksgeeksforgeeks" ;
console.log(distinctSimilarSubstrings(str));
|
Time Complexity: O(N^2)
Auxiliary Space: O(N)
Last Updated :
13 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...