给定一个大小为N的字符串S ,任务是在不改变元音位置的情况下对字符串进行排序。
例子:
Input: S = “geeksforgeeks”
Output: feeggkokreess
Explanation:
The consonants present in the string are gksfrgks. Sorting the consonants modifies their sequence to fggkkrss.
Now, update the string by placing the sorted consonants in those positions.
Input: S = “apple”
Output: alppe
处理方法:按照以下步骤解决问题:
- 初始化一个字符串,比如temp 。
- 遍历字符串S的字符
- 如果当前字符是辅音,则将该字符插入到temp 中。
- 按字典顺序对字符串temp进行排序。
- 初始化一个指针,比如ptr = 0 ,指向字符串temp 中的当前字符。
- 现在,遍历字符串S,并且与温度[PTR]替换字符串S的每个辅音。增加ptr 。
下面是上述方法的实现:
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.
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 temp = new List();
// 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
输出:
feeggkokreess
时间复杂度: O(N logN)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live