Check whether second string can be formed from characters of first string used any number of times | Java implementation of the above approach ; Function to check if str2 can be made by characters of str1 or not ; To store the occurrence of every character ; Length of the two strings ; Assume that it is possible to compose the string str2 from str1 ; Iterate over str1 ; Store the presence of every character ; Iterate over str2 ; Ignore the spaces ; Check for the presence of character in str1 ; If it is possible to make str2 from str1 ; Driver Code ; Given strings ; Function Call
import java . util . Arrays ; class GFG { public static void isPossible ( String str1 , String str2 ) { int arr [ ] = new int [ 256 ] ; Arrays . fill ( arr , 0 ) ; int l1 = str1 . length ( ) ; int l2 = str2 . length ( ) ; int i , j ; boolean possible = true ; for ( i = 0 ; i < l1 ; i ++ ) { arr [ str1 . charAt ( i ) ] = 1 ; } for ( i = 0 ; i < l2 ; i ++ ) { if ( str2 . charAt ( i ) != ' ▁ ' ) { if ( arr [ str2 . charAt ( i ) ] == 1 ) continue ; else { possible = false ; break ; } } } if ( possible ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String args [ ] ) { String str1 = " we ▁ all ▁ love ▁ geeksforgeeks " ; String str2 = " we ▁ all ▁ love ▁ geeks " ; isPossible ( str1 , str2 ) ; } }