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

📅  最后修改于: 2021-09-06 05:54:01             🧑  作者: Mango

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

例子:

方法:这个想法是将字符串S中存在的所有元音存储在另一个字符串,比如vave 。按字母顺序对字符串誓言进行排序。从一开始遍历字符串S和与许愿[J]如果S [i]为一个元音,并且递增j通过1.按照下列步骤进行更换S [I]来解决这个问题:

  • 初始化字符串誓言以存储字符串S 中存在的所有元音。
  • 遍历字符串S并检查当前字符S[i]是否为元音,如果为真,则将S[i]推入到vue 中
  • 按字母顺序对字符串v进行排序并初始化一个变量,例如j0
  • 再次使用变量i遍历字符串S ,如果当前字符S[i]是元音,则将S[i]替换为vw[j]并将j增加1
  • 经过以上步骤,打印出字符串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


Javascript


输出:
geeksfergeoks

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live