Count of Substrings with at least K pairwise Distinct Characters having same Frequency | Java 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
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 ) ) ; } }