Frequency of smallest character in first sentence less than that of second sentence | Java program for the above approach ; Function to count the frequency of minimum character ; Sort the string s ; Return the count with smallest character ; Function to count number of frequency of smallest character of string arr1 [ ] is less than the string in arr2 [ ] ; To store the frequency of smallest character in each string of arr2 ; Traverse the arr2 [ ] ; Count the frequency of smallest character in string s ; Append the frequency to freq [ ] ; Sort the frequency array ; Traverse the array arr1 [ ] ; Count the frequency of smallest character in string s ; find the element greater than f ; Find the count such that arr1 [ i ] < arr2 [ j ] ; Print the count ; Driver Code ; Function Call
import java . io . * ; import java . util . * ; class GFG { static int countMinFreq ( String s ) { char [ ] tempArray = s . toCharArray ( ) ; Arrays . sort ( tempArray ) ; s = new String ( tempArray ) ; int x = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( s . charAt ( i ) == s . charAt ( 0 ) ) x ++ ; return x ; } static void countLessThan ( List < String > arr1 , List < String > arr2 ) { List < Integer > freq = new ArrayList < Integer > ( ) ; for ( String s : arr2 ) { int f = countMinFreq ( s ) ; freq . add ( f ) ; } Collections . sort ( freq ) ; for ( String s : arr1 ) { int f = countMinFreq ( s ) ; int it = upper_bound ( freq , f ) ; int cnt = freq . size ( ) - it ; System . out . print ( cnt + " ▁ " ) ; } } static int upper_bound ( List < Integer > freq , int f ) { int low = 0 , high = freq . size ( ) - 1 ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( freq . get ( mid ) > f ) high = mid ; else low = mid + 1 ; } return ( freq . get ( low ) < f ) ? low ++ : low ; } public static void main ( String [ ] args ) { List < String > arr1 , arr2 ; arr1 = Arrays . asList ( new String [ ] { " yyy " , " zz " } ) ; arr2 = Arrays . asList ( new String [ ] { " x " , " xx " , " xxx " , " xxxx " } ) ; countLessThan ( arr1 , arr2 ) ; } }