Open In App

Length of recurring substring formed by repeating characters their index times in range [L, R] for Q queries

Improve
Improve
Like Article
Like
Save
Share
Report

Given a string S, and two integers L and R. The task is to find the length of the substring in the given range such that each character repeats after itself exactly k times, where k is the index of the corresponding character in the alphabet. Print desired result for q queries

Examples:

Input: s = “cbbde”, q = 3, queries[] = { { 2, 4 }, { 3, 5 }, { 1, 3 } };
Output: {8, 7, 11}
Explanation: Substring after recurring in the given range: “bbddddeeeee” .So, length of the substring = 11

Input: s = “xyyz”, q = 1, queries[] = {1, 2}
Output:  49

 

Approach: The approach involves the idea of precomputation for solving each query in O(1). 

  • First, create the string by repeating characters their index times
  • Precompute the length of recurring substring for range [0, i] for each character in the formed string.
  • With the help of a prefix array, store the sum of corresponding values, the current character points to, in the alphabets.
  • For each query, determine the length of the recurring substring and print the result in O(1)

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the length of
// recurring substring in range [l, r]
int recurringSubstring(string s, int l, int r)
{
 
    // Length of the string
    int N = s.size();
 
    // Variable to store the index of
    // the character in the alphabet
    int a[N];
    for (int i = 0; i < N; i++) {
 
        a[i] = (s[i] - 'a') + 1;
    }
 
    // Prefix array to store the sum
    int prefix[N];
    prefix[0] = a[0];
 
    for (int i = 1; i < N; i++) {
 
        prefix[i] = prefix[i - 1] + a[i];
    }
 
    l = l - 1;
    r = r - 1;
 
    // If l is greater than 0
    if (l != 0) {
 
        return prefix[r] - prefix[l - 1];
    }
 
    // If l is less or equal to 0
    else {
 
        return prefix[r];
    }
}
 
void recurSubQueries(string s,
                     pair<int, int> queries[],
                     int q)
{
    for (int i = 0; i < q; i++) {
        int l = queries[i].first;
        int r = queries[i].second;
        cout << recurringSubstring(s, l, r)
             << endl;
    }
}
 
// Driver Code
int main()
{
 
    string s = "cbbde";
    int q = 3;
    pair<int, int> queries[]
        = { { 2, 4 }, { 3, 5 }, { 1, 3 } };
 
    recurSubQueries(s, queries, q);
 
    return 0;
}


Java




// Java code for the above approach
import java.io.*;
 
class GFG
{
   
    // Function to find the length of
    // recurring substring in range [l, r]
    static int recurringSubstring(String s, int l, int r)
    {
 
        // Length of the string
        int N = s.length();
 
        // Variable to store the index of
        // the character in the alphabet
        int a[] = new int[N];
        for (int i = 0; i < N; i++) {
 
            a[i] = (s.charAt(i) - 'a') + 1;
        }
 
        // Prefix array to store the sum
        int prefix[] = new int[N];
        prefix[0] = a[0];
 
        for (int i = 1; i < N; i++) {
 
            prefix[i] = prefix[i - 1] + a[i];
        }
 
        l = l - 1;
        r = r - 1;
 
        // If l is greater than 0
        if (l != 0) {
 
            return prefix[r] - prefix[l - 1];
        }
 
        // If l is less or equal to 0
        else {
 
            return prefix[r];
        }
    }
 
    static void recurSubQueries(String s, int queries[][],
                                int q)
    {
        for (int i = 0; i < q; i++) {
            int l = queries[i][0];
            int r = queries[i][1];
            System.out.println(recurringSubstring(s, l, r));
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String s = "cbbde";
        int q = 3;
        int[][] queries = { { 2, 4 }, { 3, 5 }, { 1, 3 } };
 
        recurSubQueries(s, queries, q);
    }
}
 
// This code is contributed by Potta Lokesh


Python3




# Python code for the above approach
 
# Function to find the length of
# recurring substring in range [l, r]
def recurringSubstring(s, l, r):
   
    # Length of the string
    N = len(s);
 
    # Variable to store the index of
    # the character in the alphabet
    a = [0 for i in range(N)];
 
    for i in range(N):
        a[i] = ord(s[i]) - ord('a') + 1;
 
    # Prefix array to store the sum
    prefix = [0 for i in range(N)];
    prefix[0] = a[0];
 
    for i in range(N):
        prefix[i] = prefix[i - 1] + a[i];
 
    l = l - 1;
    r = r - 1;
 
    # If l is greater than 0
    if (l != 0):
 
        return prefix[r] - prefix[l - 1];
 
 
    # If l is less or equal to 0
    else:
 
        return prefix[r];
 
def recurSubQueries(s, queries, q):
    for i in range(q):
        l = queries[i][0];
        r = queries[i][1];
        print(recurringSubstring(s, l, r));
 
# Driver Code
if __name__ == '__main__':
    s = "cbbde";
    q = 3;
    queries = [[2, 4], [3, 5], [1, 3]];
 
    recurSubQueries(s, queries, q);
 
# This code is contributed by 29AjayKumar


C#




// C# code for the above approach
using System;
 
public class GFG
{
   
    // Function to find the length of
    // recurring substring in range [l, r]
    static int recurringSubstring(String s, int l, int r)
    {
 
        // Length of the string
        int N = s.Length;
 
        // Variable to store the index of
        // the character in the alphabet
        int []a = new int[N];
        for (int i = 0; i < N; i++) {
 
            a[i] = (s[i] - 'a') + 1;
        }
 
        // Prefix array to store the sum
        int []prefix = new int[N];
        prefix[0] = a[0];
 
        for (int i = 1; i < N; i++) {
 
            prefix[i] = prefix[i - 1] + a[i];
        }
 
        l = l - 1;
        r = r - 1;
 
        // If l is greater than 0
        if (l != 0) {
 
            return prefix[r] - prefix[l - 1];
        }
 
        // If l is less or equal to 0
        else {
 
            return prefix[r];
        }
    }
 
    static void recurSubQueries(String s, int [,]queries,
                                int q)
    {
        for (int i = 0; i < q; i++) {
            int l = queries[i,0];
            int r = queries[i,1];
            Console.WriteLine(recurringSubstring(s, l, r));
        }
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        String s = "cbbde";
        int q = 3;
        int[,] queries = { { 2, 4 }, { 3, 5 }, { 1, 3 } };
 
        recurSubQueries(s, queries, q);
    }
}
 
 
// This code is contributed by shikhasingrajput


Javascript




<script>
    // JavaScript program for the above approach
 
    // Function to find the length of
    // recurring substring in range [l, r]
    const recurringSubstring = (s, l, r) => {
 
        // Length of the string
        let N = s.length;
 
        // Variable to store the index of
        // the character in the alphabet
        let a = new Array(N).fill(0);
        for (let i = 0; i < N; i++) {
 
            a[i] = (s.charCodeAt(i) - 'a'.charCodeAt(0)) + 1;
        }
 
        // Prefix array to store the sum
        let prefix = new Array(N).fill(0);
        prefix[0] = a[0];
 
        for (let i = 1; i < N; i++) {
 
            prefix[i] = prefix[i - 1] + a[i];
        }
 
        l = l - 1;
        r = r - 1;
 
        // If l is greater than 0
        if (l != 0) {
 
            return prefix[r] - prefix[l - 1];
        }
 
        // If l is less or equal to 0
        else {
 
            return prefix[r];
        }
    }
 
    const recurSubQueries = (s, queries, q) => {
        for (let i = 0; i < q; i++) {
            let l = queries[i][0];
            let r = queries[i][1];
            document.write(`${recurringSubstring(s, l, r)}<br/>`);
        }
    }
 
    // Driver Code
    let s = "cbbde";
    let q = 3;
    let queries = [[2, 4], [3, 5], [1, 3]];
 
    recurSubQueries(s, queries, q);
 
    // This code is contributed by rakeshsahni
 
</script>


 
 

Output

8
11
7

 

Time Complexity: O(N+Q), where N is the length of the string
Auxiliary Space: O(N)

 



Last Updated : 03 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads