Count of Substrings with at least K pairwise Distinct Characters having same Frequency | C ++ Program for the above approach ; Function to find the substring with K pairwise distinct characters and with same frequency ; Stores the occurrence of each character in the substring ; Length of the string ; Iterate over the string ; Set all values at each index to zero ; Stores the count of unique characters ; Moving the substring ending at j ; Calculate the index of character in frequency array ; Increment the frequency ; Update the maximum index ; Check for both the conditions ; Return the answer ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE 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 ; }