Open In App

Rearrange a String According to the Given Indices

Last Updated : 15 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a string and a list of indices, the task is to rearrange the characters of the string based on the given indices. For example, if the string is "code" and the indices are [3, 1, 0, 2], the rearranged string should be "edoc". Let's explore various ways in which we can do this in Python.

Using list

We can initialize a new list of the same length as the string and place each character in its corresponding position based on the given indices.

Python
# Initializing string and indices
a = "code"
b = [3, 1, 0, 2]

# Initializing an empty list of the same length as the string
res = [""] * len(a)

# Placing each character in the correct position
for i in range(len(a)):
    res[b[i]] = a[i]

# Joining the list to form the rearranged string
rearranged = "".join(res)
print(rearranged)

Output
doec

Explanation:

  • We initialize a list 'res' with empty strings to store the rearranged characters.
  • We loop through the indices and place each character from the string at its correct position in the result list.
  • The list is then joined to form the rearranged string.

Let's explore some more ways and see how we can rearrange a string according to the given indices.

Using dictionary to map characters to indices

We can create a dictionary to map each index to its corresponding character and then sort the dictionary keys to construct the rearranged string.

Python
# Initializing string and indices
a = "code"
b = [3, 1, 0, 2]

# Initializing a dictionary to map indices to characters
mapping = {b[i]: a[i] for i in range(len(a))}

# Sorting the dictionary keys and forming the rearranged string
rearranged = "".join(mapping[i] for i in sorted(mapping))
print(rearranged)

Output
doec

Explanation:

  • We create a dictionary mapping to store the relationship between indices and characters.
  • By sorting the dictionary keys and accessing the corresponding characters, we construct the rearranged string.

Using zip() and sorting

We can use the zip() function to combine the indices and characters, sort them based on the indices and extract the rearranged string.

Python
# Initializing string and indices
a = "code"
b = [3, 1, 0, 2]

# Using zip to combine indices and characters
combined = zip(b, a)

# Sorting based on indices
sorted_combined = sorted(combined)

# Extracting the characters and forming the rearranged string
rearranged = "".join(x[1] for x in sorted_combined)
print(rearranged)

Output
doec

Explanation:

  • We use zip() to combine indices and characters creating pairs of the form (index, character).
  • Sorting these pairs based on the index gives the correct order.
  • We extract and join the characters to form the rearranged string.

Using nested loops

We can use a simple nested for loop to directly construct the rearranged string by matching each index with its corresponding character.

Python
# Initializing string and indices
a = "code"
b = [3, 1, 0, 2]

# Initializing an empty string to store the result
rearranged = ""

# Looping through the range of the length of the string
for i in range(len(a)):
    for j in range(len(b)):
        # If the index matches, add the character to the result
        if b[j] == i:
            rearranged += a[j]

print(rearranged)

Output
doec

Explanation: We use two loops: the outer loop iterates through the positions and the inner loop finds the character for each position.


Article Tags :
Practice Tags :

Similar Reads