The Wayback Machine - https://web.archive.org/web/20211201122804/https://www.geeksforgeeks.org/length-of-string-formed-by-repeating-each-character-in-range-l-r-of-given-string-its-lexicographic-value-times/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Length of String formed by repeating each character in range [L, R] of given string its lexicographic value times

  • Last Updated : 30 Nov, 2021

Given a string S of length N, and a range [L, R] (1 <= L, R <= N). The task is to find the length of string formed by repeating each character in the range [L, R], to its lexicographical value times.

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”, l = 2, r = 5
Output: 13
Explanation: Resultant String is formed after repeating each character in range [2, 5] as shown below: 
b repeated 2 times
b repeated 2 times
d repeated 4 times
e repeated 5 times
 



Resultant string: ‘bbbbddddeeeee’
Therefore, length of resultant string so formed is 13

Input: s = “xyyz”, l = 1, r = 2
Output:  49

 

Naive Approach: The task can be solved by forming a temporary string, appending characters repeated lexicographic times in the range [L, R], and finally, return the length of the resultant string.

Time Complexity: O((R-L+1) * 26)
Auxiliary Space: O((R-L+1) * 26)

Efficient Approach: The task can be solved with the help of a prefix array, that will store the sum of corresponding lexicographic values.
Follow the below steps to solve the problem:

  • Create a prefix array ‘prefix‘, to store the cumulative sum of the current character’s lexicographic value
  • To get the answer of given [L, R]: find the difference of prefix[R] and prefix[L-1], if L > 0 and prefix[L], if L = 0, to get the answer of the window (R-L+1).
  • Note: This approach works efficiently in case of multiple queries.

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];
    }
}
  
// Driver Code
int main()
{
    string s = "cbbde";
    int l = 2, r = 5;
  
    cout << recurringSubstring(s, l, r);
  
    return 0;
}
Output
13

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

Space Optimised Approach: The above approach can further be optimized by getting rid of the prefix array, and simply iterating over the given range, and adding the corresponding lexicographic values to the answer.
Follow the below steps to solve the problem:

  • Iterate over the range [L, R], and add the corresponding lexicographic values to the answer.
     

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();
  
    // Length of resultant string
    int ans = 0;
    for (int i = l - 1; i <= r - 1; i++) {
  
        // Add lexicographic value of s[i]
        ans += (s[i] - 'a' + 1);
    }
    return ans;
}
  
// Driver Code
int main()
{
    string s = "cbbde";
    int l = 2, r = 5;
  
    cout << recurringSubstring(s, l, r);
  
    return 0;
}
Output
13

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :