Open In App

Rearrange a string according to the given indices

Improve
Improve
Like Article
Like
Save
Share
Report

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 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++




// 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   


Java




// 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


Python3




# 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)


C#




// 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


Javascript




<script>
 
    // JavaScript program to implement the above approach
     
    // Function to convert the Strings to proper
    function ConvertStrings(s, index, n)
    {
        let a = new Array(s.length);
        a.fill('0');
        let b = new Array(s.length);
        b.fill('0');
 
        // Convert String to array
        for(let ii = 0; ii < s.length; ii++)
        {
            a[ii] = s[ii];
            b[ii] = s[ii];
        }
 
        let i = 0, j = 0;
 
        // Move characters to specified indices
        while(j < s.length && i < n)
        {
            let k = index[i];
            let temp = a[j].charCodeAt();
            b[k] = String.fromCharCode(temp);
 
            j += 1;
            i += 1;
        }
 
        let tmp = "";
 
        // Convert the list to String
        for(i = 0; i < s.length; i++)
        {
            tmp = tmp + b[i];
        }
 
        // Print the answer
        document.write(tmp +"</br>");
    }
     
    let s = "geeksforgeeks";
    let index = [ 5, 6, 7, 0, 1, 2, 8, 9, 10, 3, 4, 11, 12];
      
    let n = index.length;
      
    ConvertStrings(s.split(''), index, n);
 
</script>


Output: 

ksfeegeeorgks

 

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



Last Updated : 15 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads