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

• Last Updated : 30 Nov, 2021

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:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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 ` `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`

## 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

 ``

Output

```8
11
7```

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :