Build modified string ( string with characters only from pattern are taken )
let modifiedString = ( str ) . split ( " " ) ; for ( let i = str . length - 1 ; i >= 0 ; i -- ) if ( ! patternSet . has ( modifiedString [ i ] ) ) modifiedString . splice ( i , 1 ) ;