Check if a string can become empty by recursively deleting a given sub | Returns true if str can be made empty by recursively removing sub_str ; idx : to store starting index of sub - string found in the original string ; Erasing the found sub - string from the original string ; Driver code
function canBecomeEmpty ( str , sub_str ) { while ( str . length > 0 ) { let idx = str . indexOf ( sub_str ) ; if ( idx == - 1 ) { break ; } str = str . replace ( sub_str , " " ) ; } return ( str . length == 0 ) ; } let str = " " , sub_str = " " ; if ( canBecomeEmpty ( str , sub_str ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }