Check if end of given Binary string can be reached by choosing jump value in between given range | Function to check if it is possible to reach the end of the binary string using the given jumps ; Stores the DP states ; Stores count of indices from which it is possible to reach index i ; Traverse the given string ; Update the values of pre accordingly ; If the jump size is out of the range [ L , R ] ; Return answer ; Driver Code
const canReach = ( s , L , R ) => { let dp = new Array ( s . length ) . fill ( 1 ) ; let pre = 0 ; for ( let i = 1 ; i < s . length ; i ++ ) { if ( i >= L ) { pre += dp [ i - L ] ; } if ( i > R ) { pre -= dp [ i - R - 1 ] ; } dp [ i ] = ( pre > 0 ) && ( s [ i ] == ' ' ) ; } return dp [ s . length - 1 ] ; } let S = " " ; let L = 2 , R = 3 ; if ( canReach ( S , L , R ) ) document . write ( " " ) ; else document . write ( " " ) ;