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.
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.
- Prefixes with more a than b
- Print N-bit binary numbers having more 1’s than 0’s in all prefixes
- Check whether two strings can be made equal by increasing prefixes
- Python | Count all prefixes in given string with greatest frequency
- XOR two binary strings of unequal lengths
- Sort an array of strings according to string lengths using Map
- Sort an array of strings according to string lengths
- Queries to count distinct Binary Strings of all lengths from N to M satisfying given properties
- Find minimum shift for longest common prefix
- Check if left and right shift of any string results into given string
- Smallest String consisting of a String S exactly K times as a Substring
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's
- Sort an Array of Version Numbers
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.