Build modified string ( string with characters only from pattern are taken )
StringBuilder modifiedString = new StringBuilder ( str ) ; for ( int i = str . Length - 1 ; i >= 0 ; i -- ) if ( ! patternSet . Contains ( modifiedString [ i ] ) ) modifiedString . Remove ( i , 1 ) ;