Skip to content
Related Articles

Related Articles

Improve Article

Roll the characters of string

  • Difficulty Level : Medium
  • Last Updated : 30 Jul, 2020

Given a string s and an array roll where roll[i] represents rolling first roll[i] characters in string. We need to apply every roll[i] on string and output final string. Rolling means increasing ASCII value of character, like rolling ‘z’ would result in ‘a’, rolling ‘b’ would result in ‘c’, etc.

1 <= |s| <= 10^5
1 <= roll[i] <= 10^5


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"
Input : s = "zcza"
        roll[] = {1, 1, 3, 4}
Output : debb

Question source : Airtel Interview experience | Set 1 (FTE On-campus)

Simple Approach : keep on changing the characters of string for every roll[i]
Time Complexity = O(sizeof_roll_array * length_of_string) = O(n2)

Efficient Approach : Better approach will be create a array and store total roll operation to be performed on each character in it, and then traverse the string and perform the roll operation.

Code of this approach:


// Java code to roll the string
class StringRoller {
    public static String roll(String toRoll, int roll[]) {
        int toRollLength = toRoll.length();
        int each_character_roll[] = new int[toRollLength];
        char[] toRollCharArr = toRoll.toCharArray();
        for (int i = 0; i < roll.length; ++i) {
            int noOfRoll = roll[i];
            if (noOfRoll >= each_character_roll.length)
        IntStream.range(1, toRollLength).forEach(index -> {
            each_character_roll[index] = each_character_roll[index] + each_character_roll[index - 1];
        IntStream.range(0, toRollLength).forEach(index -> {
            toRollCharArr[index] = roll_character(toRollCharArr[index], each_character_roll[index]);
        return String.valueOf(toRollCharArr);
    public static char roll_character(char c, int roll) {
        roll = roll % 26;
        int character = c - 'a';
        return (char) ('a' + (character + roll) % 26);
    // Driver code
    public static void main(String[] args) {
        String s = "geeks";
        int k[] = {1, 2, 5};
        System.out.println(roll(s, k));
// Code is contributed by Snehashish.


Time Complexity : O(n+m)
Space Complexity : O(n)

This article is contributed by Mandeep Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :