Count of Substrings with at least K pairwise Distinct Characters having same Frequency
Last Updated :
06 May, 2021
Given a string S and an integer K, the task is to find the number of substrings which consists of at least K pairwise distinct characters having same frequency.
Examples:
Input: S = “abasa”, K = 2
Output: 5
Explanation:
The substrings in having 2 pairwise distinct characters with same frequency are {“ab”, “ba”, “as”, “sa”, “bas”}.
Input: S = “abhay”, K = 3
Output: 4
Explanation:
The substrings having 3 pairwise distinct characters with same frequency are {“abh”, “bha”, “hay”, “bhay”}.
Naive Approach: The simplest approach to solve this problem is to generate all possible substrings of the given string and check if both the conditions are satisfied. If found to be true, increase count. Finally, print count.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, follow the steps below to solve the problem:
- Check if the frequencies of each character is same. If found to be true, simply generate all the substrings to check if each character satisfies the condition of at least N pairwise distinct characters.
- Precompute the frequencies of characters to check the conditions for each substring.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int no_of_substring(string s, int N)
{
int fre[26];
int str_len;
str_len = ( int )s.length();
int count = 0;
for ( int i = 0; i < str_len; i++) {
memset (fre, 0, sizeof (fre));
int max_index = 0;
int dist = 0;
for ( int j = i; j < str_len; j++) {
int x = s[j] - 'a' ;
if (fre[x] == 0)
dist++;
fre[x]++;
max_index = max(max_index, fre[x]);
if (dist >= N && ((max_index * dist)
== (j - i + 1)))
count++;
}
}
return count;
}
int main()
{
string s = "abhay" ;
int N = 3;
cout << no_of_substring(s, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int no_of_subString(String s, int N)
{
int fre[] = new int [ 26 ];
int str_len;
str_len = ( int )s.length();
int count = 0 ;
for ( int i = 0 ; i < str_len; i++)
{
Arrays.fill(fre, 0 );
int max_index = 0 ;
int dist = 0 ;
for ( int j = i; j < str_len; j++)
{
int x = s.charAt(j) - 'a' ;
if (fre[x] == 0 )
dist++;
fre[x]++;
max_index = Math.max(max_index, fre[x]);
if (dist >= N && ((max_index * dist) ==
(j - i + 1 )))
count++;
}
}
return count;
}
public static void main(String[] args)
{
String s = "abhay" ;
int N = 3 ;
System.out.print(no_of_subString(s, N));
}
}
|
Python3
def no_of_substring(s, N):
str_len = len (s)
count = 0
for i in range (str_len):
fre = [ 0 ] * 26
max_index = 0
dist = 0
for j in range (i, str_len):
x = ord (s[j]) - ord ( 'a' )
if (fre[x] = = 0 ):
dist + = 1
fre[x] + = 1
max_index = max (max_index, fre[x])
if (dist > = N and
((max_index * dist) = = (j - i + 1 ))):
count + = 1
return count
s = "abhay"
N = 3
print (no_of_substring(s, N))
|
C#
using System;
class GFG{
static int no_of_subString(String s, int N)
{
int []fre = new int [26];
int str_len;
str_len = ( int )s.Length;
int count = 0;
for ( int i = 0; i < str_len; i++)
{
fre = new int [26];
int max_index = 0;
int dist = 0;
for ( int j = i; j < str_len; j++)
{
int x = s[j] - 'a' ;
if (fre[x] == 0)
dist++;
fre[x]++;
max_index = Math.Max(max_index, fre[x]);
if (dist >= N && ((max_index * dist) ==
(j - i + 1)))
count++;
}
}
return count;
}
public static void Main(String[] args)
{
String s = "abhay" ;
int N = 3;
Console.Write(no_of_subString(s, N));
}
}
|
Javascript
<script>
function no_of_subString(s , N)
{
var fre = Array.from({length: 26}, (_, i) => 0);
var str_len;
str_len = parseInt(s.length);
var count = 0;
for (i = 0; i < str_len; i++)
{
fre = Array(26).fill(0);
var max_index = 0;
var dist = 0;
for (j = i; j < str_len; j++)
{
var x = s.charAt(j).charCodeAt(0) - 'a' .charCodeAt(0);
if (fre[x] == 0)
dist++;
fre[x]++;
max_index = Math.max(max_index, fre[x]);
if (dist >= N && ((max_index * dist) ==
(j - i + 1)))
count++;
}
}
return count;
}
var s = "abhay" ;
var N = 3;
document.write(no_of_subString(s, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...