Queries on subsequence of string | Javascript program to answer subsequence queries for a given string . ; Precompute the position of each character from each position of String S ; Computing position of each character from each position of String S ; Print " Yes " if T is subsequence of S , else " No " ; Traversing the string T ; If next position is greater than length of S set flag to false . ; Setting position of next character ; Driver Code
let MAX = 10000 ; let CHAR_SIZE = 26 ; function precompute ( mat , str , len ) { for ( let i = 0 ; i < CHAR_SIZE ; ++ i ) mat [ len ] [ i ] = len ; for ( let i = len - 1 ; i >= 0 ; -- i ) { for ( let j = 0 ; j < CHAR_SIZE ; ++ j ) mat [ i ] [ j ] = mat [ i + 1 ] [ j ] ; mat [ i ] [ str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] = i ; } } function query ( mat , str , len ) { let pos = 0 ; for ( let i = 0 ; i < str . length ; ++ i ) { if ( mat [ pos ] [ str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] >= len ) return false ; else pos = mat [ pos ] [ str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] + 1 ; } return true ; } let S = " " ; let len = S . length ; let mat = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { mat [ i ] = new Array ( CHAR_SIZE ) ; for ( let j = 0 ; j < CHAR_SIZE ; j ++ ) { mat [ i ] [ j ] = 0 ; } } precompute ( mat , S , len ) ; let get = query ( mat , " " , len ) ? " " : " " ; document . write ( get + " " ) ; get = query ( mat , " " , len ) ? " " : " " ; document . write ( get + " " ) ; get = query ( mat , " " , len ) ? " " : " " ; document . write ( get + " " ) ; get = query ( mat , " " , len ) ? " " : " " ; document . write ( get + " " ) ;