This repository contains the implementation of the Shifting Letters problem in multiple languages (C++, Java, JavaScript, Python, and Go). Below is the detailed step-by-step explanation for each implementation.
-
Initialize Variables:
- Create a difference array
diff
with sizen + 1
(where (n) is the length of the string). - Initialize all elements in the array to
0
.
- Create a difference array
-
Build the Difference Array:
- Iterate through the
shifts
array. - For each shift, calculate the range
[start, end]
and direction (forward or backward). - Update the
start
andend + 1
indices in the difference array based on the direction of the shift.
- Iterate through the
-
Calculate Cumulative Shifts:
- Iterate through the difference array to calculate the cumulative sum.
- Normalize the cumulative shift to always stay within the range
[0, 25]
using modular arithmetic.
-
Apply the Shifts:
- Iterate through the string.
- Modify each character based on the cumulative shift value, ensuring wrap-around in the alphabet.
-
Return Result:
- Construct and return the modified string after applying all the shifts.
-
Initialize Variables:
- Create an array
diff
with sizen + 1
to represent the difference array. - Initialize all elements to
0
.
- Create an array
-
Build the Difference Array:
- Loop through the
shifts
array. - For each operation, adjust the difference array based on the range
[start, end]
and direction.
- Loop through the
-
Calculate Cumulative Shifts:
- Use a running sum to calculate the total shift for each character.
- Normalize the shift using modular arithmetic to handle wrap-around.
-
Apply the Shifts:
- Convert the string to a character array.
- Modify each character based on the cumulative shift, and update the array.
-
Return Result:
- Convert the character array back to a string and return it.
-
Initialize Variables:
- Create an array
diff
of sizen + 1
initialized with zeros.
- Create an array
-
Build the Difference Array:
- Iterate through the
shifts
array. - For each shift operation, adjust the start and end indices of the difference array.
- Iterate through the
-
Calculate Cumulative Shifts:
- Compute the running sum for the difference array.
- Normalize the shift values using modular arithmetic to stay within the alphabet range.
-
Apply the Shifts:
- Convert the string into an array of characters for easier manipulation.
- Apply the cumulative shift to each character, wrapping around within the alphabet.
-
Return Result:
- Join the modified character array back into a string and return it.
-
Initialize Variables:
- Create a difference array
diff
of sizen + 1
, initialized to zeros.
- Create a difference array
-
Build the Difference Array:
- Loop through the
shifts
array. - For each shift, update the difference array based on the range
[start, end]
and direction.
- Loop through the
-
Calculate Cumulative Shifts:
- Use a running sum to calculate the total shift for each character.
- Normalize the shifts to ensure they stay within
[0, 25]
using modular arithmetic.
-
Apply the Shifts:
- Convert the string into a list of characters.
- Apply the cumulative shift to each character, wrapping around as needed.
-
Return Result:
- Join the modified list of characters into a string and return it.
-
Initialize Variables:
- Create a slice
diff
of sizen + 1
initialized to zeros.
- Create a slice
-
Build the Difference Array:
- Iterate through the
shifts
slice. - For each operation, adjust the
start
andend + 1
indices in the difference slice.
- Iterate through the
-
Calculate Cumulative Shifts:
- Use a running sum to compute the cumulative shifts for each index.
- Normalize the shift values using modular arithmetic to wrap around within the alphabet.
-
Apply the Shifts:
- Convert the string to a slice of bytes for easier manipulation.
- Modify each byte based on the cumulative shift, ensuring wrap-around within the alphabet.
-
Return Result:
- Convert the modified byte slice back to a string and return it.
- Efficiency: All implementations use the difference array technique to aggregate shifts efficiently, achieving a time complexity of (O(n + m)), where (n) is the length of the string and (m) is the number of shifts.
- Normalization: Modular arithmetic ensures all calculations stay within the bounds of the alphabet.