Count strings from given array having all characters appearing in a given string | Java program to implement the above approach ; Function to count the number of Strings from an array having all characters appearing in the String S ; Initialize a set to store all distinct characters of String S ; Traverse over String S ; Insert characters into the Set ; Stores the required count ; Traverse the array ; Traverse over String arr [ i ] ; Check if character in arr [ i ] [ j ] is present in the String S or not ; Increment the count if all the characters of arr [ i ] are present in the String S ; Finally , print the count ; Driver code
import java . util . * ; class GFG { static int countStrings ( String S , String [ ] list ) { HashSet < Character > valid = new HashSet < Character > ( ) ; for ( char x : S . toCharArray ( ) ) { valid . add ( x ) ; } int cnt = 0 ; for ( int i = 0 ; i < list . length ; i ++ ) { int j = 0 ; for ( j = 0 ; j < list [ i ] . length ( ) ; j ++ ) { if ( valid . contains ( list [ i ] . charAt ( j ) ) ) continue ; else break ; } if ( j == list [ i ] . length ( ) ) cnt ++ ; } return cnt ; } public static void main ( String [ ] args ) { String [ ] arr = { " ab " , " aab " , " abaaaa " , " bbd " } ; String S = " ab " ; System . out . print ( countStrings ( S , arr ) + "NEW_LINE"); } }