Roll the characters of a String | Practice | GeeksforGeeks

Given a string S and an array roll where roll[i] represents rolling first roll[i] characters in the string, the task is to apply every roll[i] on the string and output the final string. Rolling means increasing the ASCII value of character, like rolling ‘z’ would result in ‘a’, rolling ‘b’ would result in ‘c’, etc.
 
Example 1:
Input: s = "bca"
roll[] = {1, 2, 3} 
Output: eeb
Explanation: arr[0] = 1 means roll 
first character of string -> cca
arr[1] = 2 means roll 
first two characters of string -> dda
arr[2] = 3 means roll
first three characters of string -> eeb
So final ans is "eeb".
 
Example 2:
Input: s = "zcza"
roll[] = {1, 1, 3, 4}
Explanation: debb
 
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function findRollOut() that takes String S, array roll, and integer N as parameters and returns the modified string.

Note- The length of the array roll and length of the string are equal.
 
Expected Time Complexity: O(N). 
Expected Auxiliary Space: O(N).
 
Constraints:
1 ≤ N ≤ 107
0
chessnoobdj4 months ago
Easiest c++ 8 lines
string findRollOut(string s, long long arr[], int n)
	{
	    long long int hash[n+2] = {0};
	    for(int i=0; i<n; i++)
	        hash[arr[i]]++;
	    for(int i=n; i>=0; i--)
	        hash[i] += hash[i+1];
	    for(int i=1; i<=n; i++)
	        s[i-1] = ((s[i-1]-'a')+hash[i])%26 + 'a';
	    return s;
	}
0
pandasibasis747 months ago
 string findRollOut(string s, long long arr[], int n){    // Your code goes here        int brr[n+1] = {0};for(int i = 0; i < n; i++){ brr[0] += 1; brr[arr[i]] -= 1;}for(int i = 1; i < n; i++){ brr[i] += brr[i-1];}char ch[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};for(int i = 0; i < n; i++){ int x = brr[i] % 26; int y = s[i] - 97; s[i] = ch[(x+y)%26];}        return s;}
0
mathav raj1 year ago
mathav raj
https://uploads.disquscdn.c...
-1
Pramod Vajire2 years ago
Pramod Vajire
Ahjj
0
rojan sudev2 years ago
rojan sudev
O(n) Codehttps://ide.geeksforgeeks.o...
0
Anish M N2 years ago
Anish M N
https://uploads.disquscdn.c...
0
Shivam Kumar Jha2 years ago
Shivam Kumar Jha
accepted soin and easy to understanc (cpp)https://ide.geeksforgeeks.o...
0
Bicky2 years ago
Bicky
simple cpp soln 0.06s
https://ide.geeksforgeeks.o...
0
MD. TASNIN TANVIR2 years ago
MD. TASNIN TANVIR
cpp solutionhttps://ide.geeksforgeeks.o...
0
Lohit Jat2 years ago
Lohit Jat
How this is happening?For Input:12bca3 3Output of Online Judge is:dea
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.