Count number of indices such that s [ i ] = s [ i + 1 ] : Range queries | Java program to find substring with ; Function to create prefix array ; Function to return the result of the query ; Driver Code ; Query 1 ; Query 2
import java . io . * ; 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 . charAt ( i - 1 ) == s . charAt ( i ) ) pref [ i ] ++ ; } } static int query ( int pref [ ] , int l , int r ) { return pref [ r ] - pref [ l ] ; } public static void main ( String [ ] args ) { String s = " ggggggg " ; int n = s . length ( ) ; int pref [ ] = new int [ n ] ; preCompute ( n , s , pref ) ; int l = 1 ; int r = 2 ; System . out . println ( query ( pref , l , r ) ) ; l = 1 ; r = 5 ; System . out . println ( query ( pref , l , r ) ) ; } }