text: Count number of indices such that s [ i ] = s [ i + 1 ] : Range queries | C # program to find substring with ; Function to create prefix array ; Function to return the result of the query ; Driver Code ; Query 1 ; Query 2
code: using System ; class GFG { static void preCompute ( int n , string s , int [ ] pref ) { pref [ 0 ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { pref [ i ] = pref [ i - 1 ] ; if ( s [ i - 1 ] == s [ i ] ) pref [ i ] ++ ; } } static int query ( int [ ] pref , int l , int r ) { return pref [ r ] - pref [ l ] ; } public static void Main ( ) { string s = " ggggggg " ; int n = s . Length ; int [ ] pref = new int [ n ] ; preCompute ( n , s , pref ) ; int l = 1 ; int r = 2 ; Console . WriteLine ( query ( pref , l , r ) ) ; l = 1 ; r = 5 ; Console . WriteLine ( query ( pref , l , r ) ) ; } }