Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to get final string after shifting characters with given number of positions in Python
Suppose we have a lowercase string s and another list of integers called shifts whose length is same as the length of s. Here each element in shifts[i] indicates it to shift the first i + 1 letters of s by shifts[i] positions. If shifting crosses 'z' it will wrap up to 'a'. We have to find the resulting string after applying shifts to s.
So, if the input is like s = "tomato" shifts = [2, 5, 2, 3, 7, 4], then the output will be "qjcoes". After shifting first character 2 places, it will be 't' to 'v', so string is "vomato". After that first two characters 5 places, the string now will be "atmato". Like that finally the string will be "qjcoes".
Algorithm
To solve this, we will follow these steps ?
- start := ASCII of "a"
- res := a list of the ASCII of (i - start) for each i in s
- for i in range size of shifts - 2 to 0, decrease by 1, do
- shifts[i] := shifts[i] + shifts[i + 1]
- for i in range 0 to size of s - 1, do
- c := (res[i] + shifts[i]) mod 26
- res[i] := character with ASCII (c + start)
- join the letters res into a string and return
Example
Let us see the following implementation to get better understanding ?
def solve(s, shifts):
start = ord("a")
res = [ord(i) - start for i in s]
for i in range(len(shifts) - 2, -1, -1):
shifts[i] += shifts[i + 1]
for i in range(len(s)):
c = (res[i] + shifts[i]) % 26
res[i] = chr(c + start)
return "".join(res)
s = "tomato"
shifts = [2, 5, 2, 3, 7, 4]
print(solve(s, shifts))
qjcoes
How It Works
The algorithm uses a reverse accumulation approach. First, it converts each character to its numeric position (0-25). Then it accumulates the shifts from right to left, so each position knows the total shifts it will receive. Finally, it applies the shifts with modulo 26 to handle wrapping and converts back to characters.
Step-by-Step Example
def solve_with_steps(s, shifts):
print(f"Original string: {s}")
print(f"Shifts array: {shifts}")
start = ord("a")
res = [ord(i) - start for i in s]
print(f"Character positions: {res}")
# Accumulate shifts from right to left
for i in range(len(shifts) - 2, -1, -1):
shifts[i] += shifts[i + 1]
print(f"Accumulated shifts: {shifts}")
# Apply shifts
for i in range(len(s)):
c = (res[i] + shifts[i]) % 26
res[i] = chr(c + start)
result = "".join(res)
print(f"Final string: {result}")
return result
s = "abc"
shifts = [3, 5, 9]
solve_with_steps(s, shifts)
Original string: abc Shifts array: [3, 5, 9] Character positions: [0, 1, 2] Accumulated shifts: [17, 14, 9] Final string: rpo
Conclusion
This algorithm efficiently solves the character shifting problem by accumulating shifts from right to left and applying modulo arithmetic for wrapping. The time complexity is O(n) where n is the length of the string.
