Number of strings that satisfy the given condition | Java implementation of the approach ; Function to return the count of valid strings ; Set to store indices of valid strings ; Find the maximum digit for current position ; Add indices of all the strings in the set that contain maximal digit ; Return number of strings in the set ; Driver code
import java . util . * ; class GfG { static int countStrings ( int n , int m , String s [ ] ) { HashSet < Integer > ind = new HashSet < Integer > ( ) ; for ( int j = 0 ; j < m ; j ++ ) { int mx = 0 ; for ( int i = 0 ; i < n ; i ++ ) mx = Math . max ( mx , ( int ) ( s [ i ] . charAt ( j ) - '0' ) ) ; for ( int i = 0 ; i < n ; i ++ ) if ( s [ i ] . charAt ( j ) - '0' == mx ) ind . add ( i ) ; } return ind . size ( ) ; } public static void main ( String [ ] args ) { String s [ ] = { "223" , "232" , "112" } ; int m = s [ 0 ] . length ( ) ; int n = s . length ; System . out . println ( countStrings ( n , m , s ) ) ; } }