Lexicographically smallest substring with maximum occurrences containing a ' s ▁ and ▁ b ' s only | CPP 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxFreq ( string s , int a , int b ) { int fre [ 10 ] = { 0 } ; int n = s . size ( ) ; if ( a > b ) swap ( a , b ) ; for ( int i = 0 ; i < n ; i ++ ) fre [ s [ i ] - '0' ] ++ ; if ( fre [ a ] == 0 and fre [ b ] == 0 ) return -1 ; else if ( fre [ a ] >= fre [ b ] ) return a ; else return b ; } int main ( ) { int a = 4 , b = 7 ; string s = "47744" ; cout << maxFreq ( s , a , b ) ; return 0 ; }