Algorithm


Problem Name: 848. Shifting Letters

Problem Link: https://leetcode.com/problems/shifting-letters/ 

 

You are given a string s of lowercase English letters and an integer array shifts of the same length.

Call the shift() of a letter, the next letter in the alphabet, (wrapping around so that 'z' becomes 'a').

  • For example, shift('a') = 'b', shift('t') = 'u', and shift('z') = 'a'.

Now for each shifts[i] = x, we want to shift the first i + 1 letters of s, x times.

Return the final string after all such shifts to s are applied.

 

Example 1:

Input: s = "abc", shifts = [3,5,9]
Output: "rpl"
Explanation: We start with "abc".
After shifting the first 1 letters of s by 3, we have "dbc".
After shifting the first 2 letters of s by 5, we have "igc".
After shifting the first 3 letters of s by 9, we have "rpl", the answer.

Example 2:

Input: s = "aaa", shifts = [1,2,3]
Output: "gfd"

 

Constraints:

  • 1 <= s.length <= 105
  • s consists of lowercase English letters.
  • shifts.length == s.length
  • 0 <= shifts[i] <= 109

 

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    string shiftingLetters(string S, vector<int>& shifts) {
        long sum = 0;
        for(int i = S.size() - 1; i >= 0; sum += shifts[i--]) S[i] = 'a' + (S[i] - 'a' + sum + shifts[i]) % 26;
        return S;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "abc", shifts = [3,5,9]

Output

x
+
cmd
"rpl"

#2 Code Example with Java Programming

Code - Java Programming


class Solution {
  public String shiftingLetters(String s, int[] shifts) {
    long totalShifts = 0;
    StringBuilder sb = new StringBuilder();
    for (int i = shifts.length - 1; i >= 0; i--) {
      totalShifts += shifts[i];
      sb.append(getShiftedChar(s.charAt(i), totalShifts));
    }
    return sb.reverse().toString();
  }
  
  private char getShiftedChar(char c, long shifts) {
    long newAscii = ((int) c) + (shifts % 26);
    return (char) (newAscii > 122 ? (97 + (newAscii - 122) - 1) : newAscii);
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "abc", shifts = [3,5,9]

Output

x
+
cmd
"rpl"

#3 Code Example with Javascript Programming

Code - Javascript Programming


const shiftingLetters = function(S, shifts) {
  let suffixSum = 0,
    result = "";
  for (let i = S.length - 1; i >= 0; i--) {
    suffixSum += shifts[i];
    let ascii = S[i].charCodeAt() - 97;
    result = String.fromCharCode(97 + ((ascii + suffixSum) % 26)) + result;
  }
  return result;
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "aaa", shifts = [1,2,3]

Output

x
+
cmd
"gfd"

#4 Code Example with Python Programming

Code - Python Programming


class Solution:
    def shiftingLetters(self, S, shifts):
        sm, res = sum(shift % 26 for shift in shifts) % 26, ""
        for i, s in enumerate(shifts):
            move, sm = ord(S[i]) + sm % 26, sm - s
            res += chr(move > 122 and move - 26 or move)
        return res
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "aaa", shifts = [1,2,3]

Output

x
+
cmd
"gfd"
Advertisements

Demonstration


Previous
#847 Leetcode Shortest Path Visiting All Nodes Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#849 Leetcode Maximize Distance to Closest Person Solution in C, C++, Java, JavaScript, Python, C# Leetcode