848. Shifting Letters LeetCode Solution

In this guide, you will get 848. Shifting Letters LeetCode Solution with the best time and space complexity. The solution to Shifting Letters problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Shifting Letters solution in C++
  4. Shifting Letters solution in Java
  5. Shifting Letters solution in Python
  6. Additional Resources
848. Shifting Letters LeetCode Solution image

Problem Statement of 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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

848. Shifting Letters LeetCode Solution in C++

class Solution {
 public:
  string shiftingLetters(string s, vector<int>& shifts) {
    string ans;

    for (int i = shifts.size() - 2; i >= 0; --i)
      shifts[i] = (shifts[i] + shifts[i + 1]) % 26;

    for (int i = 0; i < s.length(); ++i)
      ans += (s[i] - 'a' + shifts[i]) % 26 + 'a';

    return ans;
  }
};
/* code provided by PROGIEZ */

848. Shifting Letters LeetCode Solution in Java

class Solution {
  public String shiftingLetters(String s, int[] shifts) {
    StringBuilder sb = new StringBuilder();

    for (int i = shifts.length - 2; i >= 0; --i)
      shifts[i] = (shifts[i] + shifts[i + 1]) % 26;

    for (int i = 0; i < s.length(); ++i)
      sb.append((char) ((s.charAt(i) - 'a' + shifts[i]) % 26 + 'a'));

    return sb.toString();
  }
}
// code provided by PROGIEZ

848. Shifting Letters LeetCode Solution in Python

class Solution:
  def shiftingLetters(self, s: str, shifts: list[int]) -> str:
    ans = []

    for i in reversed(range(len(shifts) - 1)):
      shifts[i] += shifts[i + 1]

    for c, shift in zip(s, shifts):
      ans.append(chr((string.ascii_lowercase.index(c) + shift) % 26 + ord('a')))

    return ''.join(ans)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.