Subsequence queries after removing substrings | arrays to store results of preprocessing ; function to preprocess the strings ; initialize it as 0. ; store subsequence count in forward direction ; store number of matches till now ; store subsequence count in backward direction ; store number of matches till now ; function that gives the output ; length of remaining String A is less than B 's length ; Driver Code ; two queries
let fwd = new Array ( 100 ) ; let bwd = new Array ( 100 ) ; function preProcess ( a , b ) { let n = a . length ; let j = 0 ; for ( let i = 1 ; i <= a . length ; i ++ ) { if ( j < b . length && a [ i - 1 ] == b [ j ] ) { j ++ ; } fwd [ i ] = j ; } j = 0 ; for ( let i = a . length ; i >= 1 ; i -- ) { if ( j < b . length && a [ i - 1 ] == b [ b . length - j - 1 ] ) { j ++ ; } bwd [ i ] = j ; } } function query ( a , b , x , y ) { if ( ( x - 1 + a . length - y ) < b . length ) { document . write ( " " ) ; return ; } if ( fwd [ x - 1 ] + bwd [ y + 1 ] >= b . length ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let a = " " , b = " " ; preProcess ( a , b ) ; let x = 2 , y = 5 ; query ( a , b , x , y ) ; x = 3 ; y = 6 ; query ( a , b , x , y ) ;