Print list items containing all characters of a given word | Java program to print all strings that contain all characters of a word ; prints list items having all characters of word ; * Since calloc is used , map [ ] is initialized as 0 ; Set the values in map ; Get the length ( ) of given word ; * Check each item of list if has all characters of word ; * unset the bit so that strings like sss not printed ; Set the values in map for next item ; Driver code
class GFG { static final int NO_OF_CHARS = 256 ; static void print ( String [ ] list , String word , int list_size ) { int [ ] map = new int [ NO_OF_CHARS ] ; int i , j , count , word_size ; for ( i = 0 ; i < word . length ( ) ; i ++ ) map [ word . charAt ( i ) ] = 1 ; word_size = word . length ( ) ; for ( i = 0 ; i < list_size ; i ++ ) { for ( j = 0 , count = 0 ; j < list [ i ] . length ( ) ; j ++ ) { if ( map [ list [ i ] . charAt ( j ) ] > 0 ) { count ++ ; map [ list [ i ] . charAt ( j ) ] = 0 ; } } if ( count == word_size ) System . out . println ( list [ i ] ) ; for ( j = 0 ; j < word . length ( ) ; j ++ ) map [ word . charAt ( j ) ] = 1 ; } } public static void main ( String [ ] args ) { String str = " sun " ; String [ ] list = { " geeksforgeeks " , " unsorted " , " sunday " , " just " , " sss " } ; print ( list , str , 5 ) ; } }