Check whether second string can be formed from characters of first string used any number of times | 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 ; Given strings ; Function Call
function isPossible ( str1 , str2 ) { let arr = new Array ( 256 ) . fill ( 0 ) ; let l1 = str1 . length ; let l2 = str2 . length ; let i , j ; let possible = true ; for ( i = 0 ; i < l1 ; i ++ ) { arr [ str1 [ i ] ] = 1 ; } for ( i = 0 ; i < l2 ; i ++ ) { if ( str2 [ i ] != " " ) { if ( arr [ str2 [ i ] ] == 1 ) continue ; else { possible = false ; break ; } } } if ( possible ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let str1 = " " ; let str2 = " " ; isPossible ( str1 , str2 ) ;