I am trying to work through this Leetcode challenge. Basically, this is the problem:
I am given a string codeleet
and list if integers [4,5,6,7,0,2,1,3]
and I have to sort the indeces from 0 to n and thereby re-arranging the letters to get leetcode
. This is my code. I am just appending every letter from s
to output
by making sure I am reaching the ith element of the letter corresponding to the indices:
class Solution:
def restoreString(self, s: str, indices: List[int]) -> str:
output = ''
# append to output each character starting from index 0
for i in range(0, len(indices)):
output += s[indices[i]]
print(output)
But this is the output of the test case:
leetcdoe
It rearranges most of it correctly but then it messes up. Why is this the case?