Shifting Letters of a String | String Problem | LeetCode 848
4 years ago Lalit Bhagtani 2
Problem Statement
You have given an input string of lowercase characters and an integer array shifts, perform the shift operation on each and every character of input string and return the modified string after all the shift operations are executed.
Shift operation of a letter, results the next letter of the alphabet ( wrapping applied in case of ‘z’ ).
Shift operation is defined as :-
shift[i] = x, shift the first i+1 letters of input string by x times.
Example
Input :- String = "abcd", Shift = [1, 3, 4, 5] Output :- "nnli"
Input :- String = "abcd", Shift = [3, 5, 9, 1] Output :- "sqme"
Solution
This problem can be solved in following steps :-
- Traverse the Shift array from the end (n-1, where n is the length of an array) to start (index 0). Add value at the previous index to the value at the current index and take modulus by 26 (Total number of characters in English). This step is executed to find out the total number of shift operations to be performed on each character of the input string.
- Create an array of characters from the input string.
- Traverse the String array from the start (index 0) to end (n-1, where n is the length of an array). Perform the shift operation on each and every character of an array.
- Convert modified array of characters to string and return it.
Program
public class Main { public static void main(String[] args) { Main main = new Main(); String result = main.shiftingLetters("abcd", new int[] {3, 5, 9, 1}); System.out.print(result); } /* Solution */ public String shiftingLetters(String S, int[] shifts) { int previous = 0; for(int i=shifts.length-1; i>=0; i--){ shifts[i] = (shifts[i] + previous) % 26; previous = shifts[i]; } char[] chars = S.toCharArray(); for(int i=0; i<chars.length; i++){ chars[i] = (char)('a' + (((int)chars[i] + shifts[i]) % 'a') % 26); } return String.valueOf(chars); } }
Result
sqme
Similar Post
That’s all for Shifting Letters of a String in Java, If you liked it, please share your thoughts in a comments section and share it with others too.
2 thoughts on “Shifting Letters of a String | String Problem | LeetCode 848”
Hi,
Thanks for the code. I wasn’t sure your thought process:
1.Why traverse the Shift array from the end?
2. Could you explain why you are doing this intuitively:
chars[i] = (char)(‘a’ + (((int)chars[i] +shifts[i]) % ‘a’) % 26);
Especially the shifts[i]
Thanks
Hi, Sorry for late reply
Let’s take an example :-
String = “abcd”, Shift = [1, 3, 4, 5]
Here, a will shift by 1 + 3 + 4 + 5 = 13 times to become n
b shift by 3 + 4 + 5 = 12 times to become n
c shift by 4 + 5 = 9 times to become l
d shift by 5 = 5 times to become i
Now, idea is to some how normalize the array such that index corresponding to a (index 0) contains 13 instead of 1. This is done so that 2nd operation, where we are doing shift operation can be done in O(n) times instead of O(n2) times.
If we start our normalization from start of the array, then for each index we have to calculate sum of all the numbers after the index. For example, for index 1, sum will be from index 1 to end of the shift array. In this case time complexity will be O(n2)
But if we start our normalization from end of the array, this can be done in O(n) time complexity.
2. I have coded it intuitively, just to save space and it looks good. It can be done in several steps.