Dada una string S de tamaño N , la tarea es ordenar la string sin cambiar la posición de las vocales.
Ejemplos:
Entrada: S = “geeksforgeeks”
Salida: feeggkokreess
Explicación:
Las consonantes presentes en la string son gksfrgks . Ordenar las consonantes modifica su secuencia a fggkkrss .
Ahora, actualice la string colocando las consonantes ordenadas en esas posiciones.Entrada: S = “manzana”
Salida: alppe
Enfoque: siga los pasos a continuación para resolver el problema:
- Inicialice una string , digamos temp .
- Iterar sobre los caracteres de la string S.
- Si el carácter actual es una consonante, inserte el carácter en temp .
- Ordene la string temporal en orden lexicográfico.
- Inicialice un puntero, digamos ptr = 0 , para apuntar al carácter actual en la string temp .
- Ahora, recorra la string S y reemplace cada consonante de la string S con temp[ptr] . Incrementar ptr .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to sort the string // leaving the vowels unchanged void sortStr(string S) { // Length of string S int N = S.size(); string temp = ""; // Traverse the string S for (int i = 0; i < N; i++) { if (S[i] != 'a' && S[i] != 'e' && S[i] != 'i' && S[i] != 'o' && S[i] != 'u') temp += S[i]; } // Sort the string temp if (temp.size()) sort(temp.begin(), temp.end()); // Pointer to traverse the // sorted string of consonants int ptr = 0; // Traverse the string S for (int i = 0; i < N; i++) { if (S[i] != 'a' && S[i] != 'e' && S[i] != 'i' && S[i] != 'o' && S[i] != 'u') S[i] = temp[ptr++]; } cout << S; } // Driver Code int main() { string S = "geeksforgeeks"; sortStr(S); return 0; }
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG{ // Function to sort the string // leaving the vowels unchanged static void sortStr(String str) { char S[] = str.toCharArray(); // Length of string S int N = S.length; ArrayList<Character> temp = new ArrayList<>(); // Traverse the string S for(int i = 0; i < N; i++) { if (S[i] != 'a' && S[i] != 'e' && S[i] != 'i' && S[i] != 'o' && S[i] != 'u') temp.add(S[i]); } // Sort the string temp if (temp.size() != 0) Collections.sort(temp); // Pointer to traverse the // sorted string of consonants int ptr = 0; // Traverse the string S for(int i = 0; i < N; i++) { if (S[i] != 'a' && S[i] != 'e' && S[i] != 'i' && S[i] != 'o' && S[i] != 'u') S[i] = temp.get(ptr++); } System.out.println(new String(S)); } // Driver Code public static void main(String[] args) { String S = "geeksforgeeks"; sortStr(S); } } // This code is contributed by Kingash
Python3
# Python3 program for the above approach # Function to sort the string # leaving the vowels unchanged def sortStr(S): # Length of string S N = len(S) temp = "" # Traverse the string S for i in range(N): if (S[i] != 'a' and S[i] != 'e' and S[i] != 'i' and S[i] != 'o'and S[i] != 'u'): temp += S[i] # Sort the string temp if (len(temp)): p = list(temp) p.sort() temp=''.join(p) # Pointer to traverse the # sorted string of consonants ptr = 0 # Traverse the string S for i in range(N): S = list(S) if (S[i] != 'a' and S[i] != 'e' and S[i] != 'i' and S[i] != 'o' and S[i] != 'u'): S[i] = temp[ptr] ptr += 1 print(''.join(S)) # Driver Code if __name__ == "__main__": S = "geeksforgeeks" sortStr(S) # This code is contributed by ukasp.
C#
// C# program for the above approach using System; using System.Collections.Generic; public class GFG{ // Function to sort the string // leaving the vowels unchanged static void sortStr(String str) { char []S = str.ToCharArray(); // Length of string S int N = S.Length; List<char> temp = new List<char>(); // Traverse the string S for(int i = 0; i < N; i++) { if (S[i] != 'a' && S[i] != 'e' && S[i] != 'i' && S[i] != 'o' && S[i] != 'u') temp.Add(S[i]); } // Sort the string temp if (temp.Count != 0) temp.Sort(); // Pointer to traverse the // sorted string of consonants int ptr = 0; // Traverse the string S for(int i = 0; i < N; i++) { if (S[i] != 'a' && S[i] != 'e' && S[i] != 'i' && S[i] != 'o' && S[i] != 'u') S[i] = temp[ptr++]; } Console.WriteLine(new String(S)); } // Driver Code public static void Main(String[] args) { String S = "geeksforgeeks"; sortStr(S); } } // This code is contributed by Amit Katiyar
Javascript
<script> // JavaScript program for the above approach // Function to sort the string // leaving the vowels unchanged function sortStr(str) { var S = str.split(''); // Length of string S var N = S.length; var temp = []; // Traverse the string S for(var i = 0; i < N; i++) { if (S[i] != 'a' && S[i] != 'e' && S[i] != 'i' && S[i] != 'o' && S[i] != 'u') temp.push(S[i]); } // Sort the string temp if (temp.length != 0) temp.sort(); // Pointer to traverse the // sorted string of consonants var ptr = 0; // Traverse the string S for(var i = 0; i < N; i++) { if (S[i] != 'a' && S[i] != 'e' && S[i] != 'i' && S[i] != 'o' && S[i] != 'u') S[i] = temp[ptr++]; } var str = ""; for(var i =0;i<S.length;i++) str+=S[i]; document.write(str); } // Driver Code var S = "geeksforgeeks"; sortStr(S); // This code is contributed by Amit Katiyar </script>
Producción:
feeggkokreess
Complejidad temporal: O(N logN)
Espacio auxiliar: O(N)
Publicación traducida automáticamente
Artículo escrito por rishavmahato348 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA