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: #include <bits/stdc++.h> NEW_LINE using namespace std ; 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 ] ++ ; } } int query ( int pref [ ] , int l , int r ) { return pref [ r ] - pref [ l ] ; } int main ( ) { string s = " ggggggg " ; int n = s . length ( ) ; int pref [ n ] ; preCompute ( n , s , pref ) ; int l = 1 ; int r = 2 ; cout << query ( pref , l , r ) << endl ; l = 1 ; r = 5 ; cout << query ( pref , l , r ) << endl ; return 0 ; }