Skip to content

2381. Shifting Letters II #1077

Answered by mah-shamim
mah-shamim asked this question in Q&A
Jan 5, 2025 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

We need to avoid shifting the characters one by one for each shift, as this would be too slow for large inputs. Instead, we can use a more optimal approach by leveraging a technique called the prefix sum.

Steps:

  1. Mark the shift boundaries: Instead of shifting each character immediately, we mark the shift effects at the start and end of each range.
  2. Apply prefix sum: After marking all the shifts, we can compute the cumulative shifts at each character using the prefix sum technique. This allows us to efficiently apply the cumulative shifts to each character.
  3. Perform the shifts: Once we know the total shift for each character, we can apply the shifts (either forward or backward) to the string.

Replies: 1 comment 2 replies

Comment options

mah-shamim
Jan 5, 2025
Maintainer Author

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Jan 5, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Jan 5, 2025
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants