# Roll the characters of string

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.

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

Examples:

```Input : s = "bca"
roll[] = {1, 2, 3}
Output : eeb

Explanation :
arr = 1 means roll first character of string -> cca
arr = 2 means roll first two characters of string -> dda
arr = 3 means roll first three characters of string -> eeb
So final ans is "eeb"

Input : s = "zcza"
roll[] = {1, 1, 3, 4}
Output : debb
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

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 apprach :

## Java

 `// Java code to roll the string ` `import` `java.util.stream.IntStream; ` ` `  `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) { ` `            ``++each_character_roll[``0``]; ` `            ``int` `noOfRoll = roll[i]; ` `            ``if` `(noOfRoll >= each_character_roll.length) ` `                ``continue``; ` `            ``else` `                ``--each_character_roll[noOfRoll]; ` `        ``} ` ` `  `        ``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. `

Output:

`jgflt`

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 contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up

Improved By : snehashish

Article Tags :
Practice Tags :

3

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.