Rearrange a string according to the given indices

Given a string S and an array index[], the task is to rearrange the string S by placing every character S[i] to position index[i].
Example 
 

Input: S = “geeksforgeeks”, index[] = {5, 6, 7, 0, 1, 2, 8, 9, 10, 3, 4, 11, 12} 
Output: ksfeegeeorgks
Input: S = “math”, index[] = {0, 1, 2, 3} 
Output: math 
 

 

Approach: 
To solve the problem, follow the steps given below: 
 

  • Convert the string S to a list of characters, since strings are immutable in nature.
  • Copy the list. Rearrange the characters in the this list according the values from index[i].
  • Convert the list to string and print the final string.

Below is the implementation of the above approach:
 



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to convert the strings
// to proper
void Convertstrings(string s, int index[],
                              int n)
{
    char a[s.length()];
    char b[s.length()];
     
    // Convert string to array
    for(int ii = 0; ii < s.length(); ii++)
    {
        a[ii] = s[ii];
        b[ii] = s[ii];
    }
     
    int i = 0, j = 0;
     
    // Move characters to specified indices
    while(j < s.length() && i < n)
    {
        int k = index[i];
        int temp = a[j];
        b[k] = temp;
         
        j += 1;
        i += 1;
    }
     
    string tmp = "";
     
    // Convert the list to string
    for(i = 0; i < s.length(); i++)
    {
        tmp += b[i];
    }
 
    // Print the answer
    cout << tmp << endl;
}
 
// Driver Code
int main()
{
    string s = "geeksforgeeks";
    int index[] = { 5, 6, 7, 0, 1, 2, 8,
                    9, 10, 3, 4, 11, 12};
     
    int n = sizeof(index) / sizeof(index[0]);
     
    Convertstrings(s, index, n);
    return 0;
}
 
// This code is contributed by rutvik_56   

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above approach
import java.util.*;
class GFG{
 
// Function to convert the Strings
// to proper
static void ConvertStrings(char []s,
                           int index[],
                           int n)
{
    char []a = new char[s.length];
    char []b = new char[s.length];
     
    // Convert String to array
    for(int ii = 0; ii < s.length; ii++)
    {
        a[ii] = s[ii];
        b[ii] = s[ii];
    }
     
    int i = 0, j = 0;
     
    // Move characters to specified indices
    while(j < s.length && i < n)
    {
        int k = index[i];
        int temp = a[j];
        b[k] = (char) temp;
         
        j += 1;
        i += 1;
    }
     
    String tmp = "";
     
    // Convert the list to String
    for(i = 0; i < s.length; i++)
    {
        tmp += b[i];
    }
 
    // Print the answer
    System.out.print(tmp +"\n");
}
 
// Driver Code
public static void main(String[] args)
{
    String s = "geeksforgeeks";
    int index[] = { 5, 6, 7, 0, 1, 2, 8,
                    9, 10, 3, 4, 11, 12};
     
    int n = index.length;
     
    ConvertStrings(s.toCharArray(), index, n);
}
}
 
// This code is contributed by Rohit_ranjan

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to implement
# the above approach
 
# Function to convert the strings
# to proper
def Convertstrings(s, index):
    a = []
    j = 0
    i = 0
 
    # Convert string to list
    for ii in str(s):
        a.append(ii)
 
    # Copy the list to another list
    b = a[:]
 
    # Move characters to specified indices
    while j < len(a) and i < len(index):
        k = index[i]
        temp = a[j]
        b[k] = temp
        j += 1
        i += 1
    s = ''
 
    # Convert the list to string
    for i in range(len(b)):
        s += b[i]
 
    # Print the answer
    print(s)
 
 
# Driver Code
s = "geeksforgeeks"
index = [5, 6, 7, 0, 1, 2, 8, 9, 10, 3, 4, 11, 12]
Convertstrings(s, index)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to convert the Strings
// to proper
static void ConvertStrings(char []s,
                           int []index,
                           int n)
{
    char []a = new char[s.Length];
    char []b = new char[s.Length];
     
    // Convert String to array
    for(int ii = 0; ii < s.Length; ii++)
    {
        a[ii] = s[ii];
        b[ii] = s[ii];
    }
     
    int i = 0, j = 0;
     
    // Move characters to specified indices
    while(j < s.Length && i < n)
    {
        int k = index[i];
        int temp = a[j];
        b[k] = (char) temp;
         
        j += 1;
        i += 1;
    }
     
    String tmp = "";
     
    // Convert the list to String
    for(i = 0; i < s.Length; i++)
    {
        tmp += b[i];
    }
 
    // Print the answer
    Console.Write(tmp +"\n");
}
 
// Driver Code
public static void Main(String[] args)
{
    String s = "geeksforgeeks";
    int []index = { 5, 6, 7, 0, 1, 2, 8,
                    9, 10, 3, 4, 11, 12};
     
    int n = index.Length;
     
    ConvertStrings(s.ToCharArray(), index, n);
}
}
 
// This code is contributed by Rajput-Ji

chevron_right


Output: 

ksfeegeeorgks





 

Time Complexity: O(N) 
Auxiliary Space: O(N)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.