📜  在不改变元音位置的情况下对字符串进行排序

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

给定大小为N的字符串S ,任务是在不更改元音位置的情况下对字符串进行排序。

例子:

方法:请按照以下步骤解决问题:

  • 初始化一个字符串,例如temp
  • 遍历字符串S的字符
  • 如果当前字符是辅音,则将该字符插入temp
  • 按字典顺序对字符串temp进行排序。
  • 初始化一个指针,例如ptr = 0 ,以指向字符串temp中的当前字符。
  • 现在,遍历字符串S,并且与温度[PTR]替换字符串S的每个辅音。增量点数

下面是上述方法的实现:

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


输出:
feeggkokreess

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