Count number of indices such that s [ i ] = s [ i + 1 ] : Range queries | Function to create prefix array ; Function to return the result of the query ; Driver Code ; Query 1 ; Query 2
function preCompute ( n , s , pref ) { pref [ 0 ] = 0 ; for ( let i = 1 ; i < n ; i ++ ) { pref [ i ] = pref [ i - 1 ] ; if ( s [ i - 1 ] == s [ i ] ) pref [ i ] ++ ; } } function query ( pref , l , r ) { return pref [ r ] - pref [ l ] ; } let s = " " ; let n = s . length ; let pref = new Array ( n ) ; preCompute ( n , s , pref ) ; let l = 1 ; let r = 2 ; document . write ( query ( pref , l , r ) + " " ) ; l = 1 ; r = 5 ; document . write ( query ( pref , l , r ) + " " ) ;