text: 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
code: < ? php function preCompute ( $ n , $ s , & $ pref ) { $ pref [ 0 ] = 0 ; for ( $ 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 ] ; } $ s = " ggggggg " ; $ n = strlen ( $ s ) ; $ pref = array_fill ( 0 , $ n , NULL ) ; preCompute ( $ n , $ s , $ pref ) ; $ l = 1 ; $ r = 2 ; echo query ( $ pref , $ l , $ r ) . " STRNEWLINE " ; $ l = 1 ; $ r = 5 ; echo query ( $ pref , $ l , $ r ) . " STRNEWLINE " ; ? >