Dada una string S de longitud N , la tarea es clasificar las vocales de la string dada en orden alfabético y colocarlas en sus respectivos índices.
Ejemplos:
Entrada: S = “geeksforgeeks”
Salida: geeksfergeoks
Explicación:
Las vocales en la string son: e, e, o, e, e
Ordenar en orden alfabético: e, e, e, e, o y reemplazar con las vocales presentes en el original cuerda.Entrada: S = “personas”
Salida: peeplo
Planteamiento: La idea es almacenar todas las vocales presentes en la string S en otra string, digamos voto . Ordene el voto de string en orden alfabético . Recorra la string S desde el principio y reemplace S[i] con vote[j] si S[i] es una vocal, e incrementando j en 1. Siga los pasos a continuación para resolver el problema:
- Inicialice un voto de string para almacenar todas las vocales presentes en la string, S .
- Atraviese la string S y verifique si el carácter actual S[i] es una vocal o no . Si es cierto, presione S[i] para votar .
- Ordene el voto de string en orden alfabético e inicialice una variable, digamos j como 0 .
- Recorra nuevamente la string S usando la variable i y si el carácter actual S[i] es una vocal, entonces reemplace S[i] con vote[j] e incremente j en 1 .
- Después de los pasos anteriores, imprima la string S como resultado.
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 arrange the vowels // in sorted order in the string // at their respective places void sortVowels(string S) { // Store the size of the string int n = S.size(); // Stores vowels of string S string vow = ""; // Traverse the string, S and push // all the vowels to string vow for (int i = 0; i < n; i++) { if (S[i] == 'a' || S[i] == 'e' || S[i] == 'i' || S[i] == 'o' || S[i] == 'u') { vow += S[i]; } } // If vow is empty, then print S // and return if (vow.size() == 0) { cout << S; return; } // Sort vow in alphabetical order sort(vow.begin(), vow.end()); int j = 0; // Traverse the string, S for (int i = 0; i < n; i++) { // Replace S[i] with vow[j] iif S[i] // is a vowel, and increment j by 1 if (S[i] == 'a' || S[i] == 'e' || S[i] == 'i' || S[i] == 'o' || S[i] == 'u') { S[i] = vow[j++]; } } // Print the string cout << S; } // Driver Code int main() { string S = "geeksforgeeks"; sortVowels(S); return 0; }
Java
// Java program for the above approach import java.util.Arrays; class GFG { // Function to arrange the vowels // in sorted order in the string // at their respective places static void sortVowels(String S) { // Store the size of the string int n = S.length(); // Stores vowels of string S String vow = ""; // Traverse the string, S and push // all the vowels to string vow for (int i = 0; i < n; i++) { if (S.charAt(i) == 'a' || S.charAt(i) == 'e' || S.charAt(i) == 'i' || S.charAt(i) == 'o' || S.charAt(i) == 'u') { vow = vow.substring(0, vow.length()) + S.charAt(i); } } // If vow is empty, then print S // and return if (vow.length() == 0) { System.out.print(S); return; } // Convert vow to char array char tempArray[] = vow.toCharArray(); // Sort vow in alphabetical order Arrays.sort(tempArray); int j = 0; // Traverse the string, S for (int i = 0; i < n; i++) { // Replace S[i] with vow[j] iif S[i] // is a vowel, and increment j by 1 if (S.charAt(i) == 'a' || S.charAt(i) == 'e' || S.charAt(i) == 'i' || S.charAt(i) == 'o' || S.charAt(i) == 'u') { S = S.substring(0, i) + tempArray[j++] + S.substring(i + 1, n); } } // Print the string System.out.print(S); } // Driver Code public static void main(String[] args) { String S = "geeksforgeeks"; sortVowels(S); } } // This code is contributed by subhammahato348.
Python3
# Python3 program for the above approach # Function to arrange the vowels # in sorted order in the string # at their respective places def sortVowels(S) : # Store the size of the string n = len(S); # Stores vowels of string S vow = ""; # Traverse the string, S and push # all the vowels to string vow for i in range(n) : if (S[i] == 'a' or S[i] == 'e' or S[i] == 'i' or S[i] == 'o' or S[i] == 'u') : vow += S[i]; # If vow is empty, then print S # and return if len(vow) == 0 : print(S,end=""); return; # Sort vow in alphabetical order vow = list(vow); vow.sort(); j = 0; # Traverse the string, S for i in range(n) : # Replace S[i] with vow[j] iif S[i] # is a vowel, and increment j by 1 if (S[i] == 'a' or S[i] == 'e' or S[i] == 'i' or S[i] == 'o' or S[i] == 'u') : S[i] = vow[j]; j += 1; # Print the string print("".join(S),end=""); # Driver Code if __name__ == "__main__" : S = "geeksforgeeks"; sortVowels(list(S)); # This code is contributed by AnkThon
C#
// C# program for the above approach using System; public class GFG { // Function to arrange the vowels // in sorted order in the string // at their respective places static void sortVowels(string S) { // Store the size of the string int n = S.Length; // Stores vowels of string S string vow = ""; // Traverse the string, S and push // all the vowels to string vow for (int i = 0; i < n; i++) { if (S[i] == 'a' || S[i] == 'e' || S[i] == 'i' || S[i] == 'o' || S[i] == 'u') { vow = vow.Substring(0, vow.Length) + S[i]; } } // If vow is empty, then print S // and return if (vow.Length == 0) { Console.Write(S); return; } // Convert vow to char array char []tempArray = vow.ToCharArray(); // Sort vow in alphabetical order Array.Sort(tempArray); int j = 0; // Traverse the string, S for (int i = 0; i < n; i++) { // Replace S[i] with vow[j] iif S[i] // is a vowel, and increment j by 1 if (S[i] == 'a' || S[i] == 'e' || S[i] == 'i' || S[i] == 'o' || S[i] == 'u') { S = S.Substring(0, i) + tempArray[j++] + S.Substring(i + 1, n - i - 1); } } // Print the string Console.Write(S); } // Driver Code public static void Main(string[] args) { string S = "geeksforgeeks"; sortVowels(S); } } // This code is contributed by AnkThon
Javascript
<script> // Javascript program for the above approach // Function to arrange the vowels // in sorted order in the string // at their respective places function sortVowels(S) { // Store the size of the string var n = S.length; // Stores vowels of string S var vow = ""; // Traverse the string, S and push // all the vowels to string vow for (var i = 0; i < n; i++) { if (S[i] == 'a' || S[i] == 'e' || S[i] == 'i' || S[i] == 'o' || S[i] == 'u') { vow += S[i]; } } // If vow is empty, then print S // and return if (vow.length == 0) { document.write( S); return; } // Sort vow in alphabetical order vow = vow.split('').sort(); var j = 0; // Traverse the string, S for (var i = 0; i < n; i++) { // Replace S[i] with vow[j] iif S[i] // is a vowel, and increment j by 1 if (S[i] == 'a' || S[i] == 'e' || S[i] == 'i' || S[i] == 'o' || S[i] == 'u') { S[i] = vow[j++]; } } // Print the string document.write( S.join('')); } // Driver Code var S = "geeksforgeeks".split(''); sortVowels(S); </script>
geeksfergeoks
Complejidad de tiempo: O(N*log N)
Espacio auxiliar: O(N)
Publicación traducida automáticamente
Artículo escrito por subhammahato348 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA