Dada una string S y una array index[] , la tarea es reorganizar la string S colocando cada carácter S[i] en la posición index[i] .
Ejemplo
Entrada: S = «geeksforgeeks», index[] = {5, 6, 7, 0, 1, 2, 8, 9, 10, 3, 4, 11, 12}
Salida: ksfeegeeorgks
Entrada: S = «matemáticas», índice[] = {0, 1, 2, 3}
Salida: matemáticas
Enfoque:
Para resolver el problema, siga los pasos que se detallan a continuación:
- Convierta la string S en una lista de caracteres, ya que las strings son inmutables por naturaleza.
- Copia la lista. Reorganice los caracteres en esta lista según los valores de index[i] .
- Convierta la lista en string e imprima la string final.
A continuación se muestra la implementación del enfoque anterior:
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>
Producción:
ksfeegeeorgks
Complejidad temporal: O(N)
Espacio auxiliar: O(N)
Publicación traducida automáticamente
Artículo escrito por simranjenny84 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA