Lexicographically smallest substring with maximum occurrences containing a ' s ▁ and ▁ b ' s only | Java program to Find the lexicographically smallest substring in a given string with maximum frequency and contains a ' s ▁ and ▁ b ' s only ; Function to Find the lexicographically smallest substring in a given string with maximum frequency and contains a ' s ▁ and ▁ b ' s only . ; To store frequency of digits ; size of string ; Take lexicographically larger digit in b ; get frequency of each character ; If no such string exits ; Maximum frequency ; Driver program
import java . io . * ; class GFG { static int maxFreq ( String s , int a , int b ) { int fre [ ] = new int [ 10 ] ; int n = s . length ( ) ; if ( a > b ) { int temp = a ; a = b ; b = temp ; } for ( int i = 0 ; i < n ; i ++ ) fre [ s . charAt ( i ) - '0' ] ++ ; if ( fre [ a ] == 0 && fre [ b ] == 0 ) return - 1 ; else if ( fre [ a ] >= fre [ b ] ) return a ; else return b ; } public static void main ( String [ ] args ) { int a = 4 , b = 7 ; String s = "47744" ; System . out . print ( maxFreq ( s , a , b ) ) ; } }