Given a string S containing letters and digits, and an integer array Shift where, and for each element of Shift array . The task is, for each Shift[i] = X, you have to shift the first i+1 letters of S, X times. Return the final string after all applying all such shift to S.
Note : Shift means cyclically increment ASCII value.
Input: S = “abc789”, Shift = [2, 5, 9]
Explanation: Starting with “abc”.
After shifting the first 1 letters of S by 2, we have “cbc”.
After shifting the first 2 letters of S by 5, we have “hgc”.
After shifting the first 3 letters of S by 9, we have “qpl”.
Input : S = “geeksforgeeks”, Shift = [ 11, 10000, 9999999 ]
Output : qdnyulaufkuug
Approach: The i-th character of S is shifted Shift[i] + Shift[i+1] + … + Shift[Shift.length – 1] times.
So we update the Shift array backwards to know exact number of shifts to be applied to each element of string S.
Traverse the given text (S) one character at a time . For each character, transform the given character as per the rule, i:e apply shift, Shift[i] times . Return the new string generated.
Time Complexity: O(N), where N is the length of string S.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Sort an array of strings according to string lengths
- Print N-bit binary numbers having more 1’s than 0’s in all prefixes
- Find minimum shift for longest common prefix
- Prefixes with more a than b
- Python | Count all prefixes in given string with greatest frequency
- Check whether two strings can be made equal by increasing prefixes
- Sort an array of strings according to string lengths using Map
- XOR two binary strings of unequal lengths
- Check if left and right shift of any string results into given string
- Construct a string that has exactly K subsequences from given string
- Check if a large number is divisible by a number which is a power of 2
- Remove leading zeros from a Number given as a string
- Minimum cost to convert given string to consist of only vowels
- Longest substring that starts with X and ends with Y
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.