Count number of indices such that s[i] = s[i+1] : Range queries
Given a string str. Now for every query consisting of two integer L and R, the task is to find the number of indices such that str[i] = str[i+1] and L ? i, i+1 ? R.
Examples:
Input: str = “ggggggggggg”, query[] = {{1, 2}, {1, 5}}
Output: 1 4
The answer is 1 for first query and 4 for second query.
The condition is true for all indices except the last one in each query.
Input: str = “geeg”, query[] = {{0, 3}}
Output: 1
The condition is true only for i = 1.
Approach: Create a prefix array pref such that pref[i] holds the count of all the indices from 1 to i-1 such that str[i] = str[i+1]. Now for every query (L, R) the result will be pref[r] – pref[l].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
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));
}
}
|
Python3
def preCompute(n, s, pref):
for i in range ( 1 ,n):
pref[i] = pref[i - 1 ]
if s[i - 1 ] = = s[i]:
pref[i] + = 1
def query(pref, l, r):
return pref[r] - pref[l]
if __name__ = = "__main__" :
s = "ggggggg"
n = len (s)
pref = [ 0 ] * n
preCompute(n, s, pref)
l = 1
r = 2
print (query(pref, l, r))
l = 1
r = 5
print (query(pref, l, r))
|
C#
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));
}
}
|
PHP
<?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 ) . "\n" ;
$l = 1;
$r = 5;
echo query( $pref , $l , $r ) . "\n" ;
?>
|
Javascript
<script>
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 = "ggggggg" ;
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)+ "<br>" );
l = 1;
r = 5;
document.write(query(pref, l, r)+ "<br>" );
</script>
|
Complexity Analysis:
- Time Complexity: O(n+k) where n is the size of the string and k is the number of queries.
- Auxiliary Space: O(n)
Last Updated :
09 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...