Skip to content

Latest commit

 

History

History
97 lines (82 loc) · 4.28 KB

File metadata and controls

97 lines (82 loc) · 4.28 KB

Hints

  1. Think about how to handle multiple range updates efficiently without actually performing each shift operation
  2. Consider using a difference array to track the net effect of all shifts at each position
  3. Remember that shifting letters is circular (e.g., 'z' + 1 = 'a')

Problem Breakdown

  1. Range Update Handling

    • How to efficiently process multiple range updates without doing them one by one
    • How to track cumulative shifts for each position
  2. Letter Shifting Logic

    • How to handle forward and backward shifts
    • How to handle circular shifts (wrapping around from 'z' to 'a' or 'a' to 'z')
    • How to compute the final position of each letter

Key Intuitions

  1. Instead of processing each shift operation separately, we can use line sweep technique to track the net effect
  2. A difference array helps us avoid processing each position in each shift operation
  3. The final shift at any position is the prefix sum of the difference array up to that position
  4. Any shift greater than 26 can be reduced to its modulo 26 due to the circular nature of the alphabet

Edge Cases

  1. Long shifts with large values (need modulo handling)
  2. Multiple overlapping shifts on the same range
  3. Shifts that cancel each other out
  4. Shifts that span the entire string
  5. Shifts with negative values after cumulative calculation

Potential Pitfalls

  1. Forgetting to handle negative shifts properly (need to add 26 to make it positive)
  2. Not considering the circular nature of letter shifts
  3. Integer overflow when accumulating shifts
  4. Incorrectly updating difference array boundaries
  5. Not properly handling the modulo operation for negative numbers

Line Sweep

This is a range update problem, we can use line sweep algorithm to solve it. For each shift, we can update the difference array:

  • Add +1 to a range if direction is forward.
  • Add -1 to a range if direction is backward.

Then compute a prefix sum of the difference array to know the final net shift at each position. Apply the shift modulo 26 to handle letter wrapping. (e.g., 'z' + 1 = 'a' or 'a' - 1 = 'z')

fun shiftingLetters(s: String, shifts: Array<IntArray>): String {
    val n = s.length
    val diff = IntArray(n + 1)
    for ((start, end, direction) in shifts) {
        if (direction == 1) {
            diff[start]++
            diff[end + 1]--
        } else {
            diff[start]--
            diff[end + 1]++
        }
    }

    var value = 0
    val ans = StringBuilder()
    for (i in 0 until s.length) {
        val c = s[i]
        value += diff[i]

        val original = c - 'a'
        // If the shift is more than 26, it's equal to shift `value % 26`, because the shift is circular
        // For example, shift = 27, original = 0 ('a'), index = (0 + 27) % 26 = 1 = 'b'
        val shift = value % 26 

        var index = 0
        if (shift >= 0) {
            index = (original + shift) % 26
        } else {
            // If the shift is negative (backward), we need to add 26 to ensure the shift is [0..25]
            // For example, shift = -1, original = 0 ('a'), index = (0 - 1 + 26) % 26 = 25 = 'z'
            index = (original + shift + 26) % 26
        }
        ans.append('a' + index)
    }
    return ans.toString()
}

Similar Problems and Variants

  1. 2381. Shifting Letters I - Simpler version with forward shifts only
  2. 370. Range Addition - Similar range update concept
  3. 1094. Car Pooling - Another application of line sweep
  4. 1589. Maximum Sum Obtained of Any Permutation - Uses similar difference array technique

Follow-up Questions

  1. How would you handle the problem if shifts could be applied in any order?
  2. What if the alphabet size was different (not just 26 letters)?
  3. What if we needed to query the state of the string after applying only the first k shifts?
  4. How would you parallelize this solution for very large strings?
  • Time Complexity: O(n + m), where n is the length of the string s, and m is the number of shifts.
  • Space Complexity: O(n).