Check whether second string can be formed from characters of first string | Javascript program to check whether second string can be formed from first string ; Create a count array and count frequencies characters in str1 . ; Now traverse through str2 to check if every character has enough counts ; Driver Code
let MAX = 256 ; function canMakeStr2 ( str1 , str2 ) { let count = new Array ( MAX ) ; count . fill ( 0 ) ; for ( let i = 0 ; i < str1 . length ; i ++ ) count [ str1 [ i ] ] ++ ; for ( let i = 0 ; i < str2 . length ; i ++ ) { if ( count [ str2 [ i ] ] == 0 ) return false ; count [ str2 [ i ] ] -- ; } return true ; } let str1 = " " ; let str2 = " " ; if ( canMakeStr2 ( str1 , str2 ) ) document . write ( " " ) ; else document . write ( " " ) ;