📌  相关文章
📜  通过按字母顺序在其各自的索引处重新排列元音来修改字符串

📅  最后修改于: 2021-04-17 14:30:57             🧑  作者: Mango

给定长度为N的字符串S ,任务是按字母顺序对给定字符串的元音进行排序,然后将其相应地放置在其各自的索引中。

例子:

方法:这个想法是将存在于字符串S中的所有元音存储在另一个字符串,例如vow 。按字母顺序对字符串vow进行排序。从头开始遍历字符串S ,如果S [i]是元音,则用vow [j]替换S [i] ,并将j递增1。请按照以下步骤解决问题:

  • 初始化字符串誓言以存储字符串S中存在的所有元音。
  • 遍历字符串S并检查当前字符S [i]是否为元音。如果发现为真,则将S [i]推入vow
  • 按字母顺序对字符串vow进行排序,并初始化一个变量,例如j0
  • 再次遍历字符串s使用变量i,并且如果当前字符S [i]为一个元音,然后通过1许愿[j]与增量Ĵ更换S [i]中
  • 完成上述步骤后,将字符串S打印为结果。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
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


输出:
geeksfergeoks

时间复杂度: O(N * log N)
辅助空间: O(N)