给定一个字符串S ,任务是计算元音必须移动的位置数,以便所有辅音都放在前面,所有元音放在最后。新字符串中辅音和元音的顺序必须相同。例子:
Input: S = “abcdefghi”
Output: 9
Explanation:
The consonants present in the string are b, c, d, f, g and h and the vowels are a, e and i. On rearrangement the final string turns out to be “bcdfghaei” and the order of the consonants and vowels is not changed.
Initially ‘a’ was at index 0 and finally it moved to index 6. No. of positions moved = 6 – 0 = 6.
Initially ‘e’ was at index 4 and finally it moved to index 7. No. of positions moved = 7 – 4 = 3.
Initially ‘i’ was at index 8 and it didn’t change its position. So no. of moves = 0.
Total number of positions moved = 6 + 3 + 0 = 9.
Input: S = “iijedf”
Output: 8
Explanation:
The consonants present in the string are j, d and f and the vowels are i, i and e. On rearrangement the final string turns out to be “jdfiie” and the order of the consonants and vowels is not changed.
‘i’ at index 0 is moved to index 3. No. of positions moved = 3 – 0 = 3.
‘i’ at index 1 is moved to index 4. No. of positions moved = 4 – 1 = 3.
‘e’ at index 3 is moved to index 5. No. of positions moved = 5 – 3 = 2.
Total number of positions moved = 3 + 3 + 2 = 8.
方法:
- 创建空字符串元音和辅音来存储给定字符串的元音和辅音。
- 遍历给定的字符串S ,如果当前字符是元音,则将其附加到字符串元音字符串,否则附加到字符串辅音字符串。
- 将辅音和元音字符串的串联存储在ans 字符串。
- 初始化 2 个指针p1和p2 ,使得 p1 指向 S 的第一个索引,而 p2 指向第一个元音出现在ans字符串的索引。
- 将计数器变量cnt初始化为 0。
- 每次在与指数P2字符索引P1相符的字符,加上P2的值– P1到CNT和递增1 P1和P2的值。
- 对每个索引重复步骤 6,直到到达ans的最后一个索引。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check whether a character
// is vowel or not
bool isvowel(char x)
{
if (x == 'a' || x == 'e' || x == 'i'
|| x == 'o' || x == 'u' || x == 'A'
|| x == 'E' || x == 'I' || x == 'O'
|| x == 'U')
return true;
else
return false;
}
// Function that creates a new string
// such that all consonants are at
// the front of the string
void movetofront(string s)
{
// To store the vowels and
// consonants in the same order
string vowels, consonants;
// To store the resultant string
string ans;
vowels = consonants = ans = "";
for (int i = 0; s[i]; i++) {
// Check if s[i] is vowel
if (isvowel(s[i])) {
vowels += s[i];
}
// Else s[i] is consonant
else {
consonants += s[i];
}
}
// concatenate the strings formed
ans = consonants + vowels;
// Pointer variables
int p1 = 0;
int p2 = consonants.size();
// Counter variable
int cnt = 0;
// Condition to check if the
// given string has only
// consonants
if (p2 == ans.size()) {
cout << 0 << endl;
return;
}
// Condition to check if the
// string has only vowels
if (ans.size() == vowels.size()) {
cout << 0 << endl;
return;
}
// Loop to find the count of
// number of positions moved
while (p2 < ans.size()) {
if (ans[p2] == s[p1]) {
cnt += p2 - p1;
p1++;
p2++;
}
else {
p1++;
}
}
cout << cnt << endl;
return;
}
// Driver Code
int main()
{
// Given string
string s = "abcdefghi";
// Function Call
movetofront(s);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check whether a character
// is vowel or not
static boolean isvowel(char x)
{
if (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u' || x == 'A' ||
x == 'E' || x == 'I' || x == 'O' ||
x == 'U')
return true;
else
return false;
}
// Function that creates a new String
// such that all consonants are at
// the front of the String
static void movetofront(String s)
{
// To store the vowels and
// consonants in the same order
String vowels, consonants;
// To store the resultant String
String ans;
vowels = consonants = ans = "";
for (int i = 0; i < s.length(); i++)
{
// Check if s.charAt(i) is vowel
if (isvowel(s.charAt(i)))
{
vowels += s.charAt(i);
}
// Else s.charAt(i) is consonant
else
{
consonants += s.charAt(i);
}
}
// concatenate the Strings formed
ans = consonants + vowels;
// Pointer variables
int p1 = 0;
int p2 = consonants.length();
// Counter variable
int cnt = 0;
// Condition to check if the
// given String has only
// consonants
if (p2 == ans.length())
{
System.out.print(0 + "\n");
return;
}
// Condition to check if the
// String has only vowels
if (ans.length() == vowels.length())
{
System.out.print(0 + "\n");
return;
}
// Loop to find the count of
// number of positions moved
while (p2 < ans.length())
{
if (ans.charAt(p2) == s.charAt(p1))
{
cnt += p2 - p1;
p1++;
p2++;
}
else
{
p1++;
}
}
System.out.print(cnt + "\n");
return;
}
// Driver Code
public static void main(String[] args)
{
// Given String
String s = "abcdefghi";
// Function Call
movetofront(s);
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 program for the above approach
# Function to check whether a character
# is vowel or not
def isvowel(x):
if (x == 'a' or x == 'e' or
x == 'i' or x == 'o' or
x == 'u' or x == 'A' or
x == 'E' or x == 'I' or
x == 'O' or x == 'U'):
return bool(True)
else:
return bool(False)
# Function that creates a new string
# such that all consonants are at
# the front of the string
def movetofront(s):
# To store the vowels and
# consonants in the same order
vowels = consonants = ans = ""
for i in range(len(s)):
# Check if s[i] is vowel
if (isvowel(s[i])):
vowels += s[i]
# Else s[i] is consonant
else:
consonants += s[i]
# concatenate the strings formed
ans = consonants + vowels
# Pointer variables
p1 = 0
p2 = len(consonants)
# Counter variable
cnt = 0
# Condition to check if the
# given string has only
# consonants
if (p2 == len(ans)):
print(0)
return
# Condition to check if the
# string has only vowels
if (len(ans) == len(vowels)):
print(0)
return
# Loop to find the count of
# number of positions moved
while (p2 < len(ans)):
if (ans[p2] == s[p1]):
cnt += p2 - p1
p1 += 1
p2 += 1
else:
p1 += 1
print(cnt)
return
# Driver code
# Given string
s = "abcdefghi"
# Function call
movetofront(s)
# This code is contributed by divyeshrabadiya07
C#
// C# program for the above approach
using System;
class GFG{
// Function to check whether a character
// is vowel or not
static bool isvowel(char x)
{
if (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u' || x == 'A' ||
x == 'E' || x == 'I' || x == 'O' ||
x == 'U')
return true;
else
return false;
}
// Function that creates a new String
// such that all consonants are at
// the front of the String
static void movetofront(String s)
{
// To store the vowels and
// consonants in the same order
String vowels, consonants;
// To store the resultant String
String ans;
vowels = consonants = ans = "";
for (int i = 0; i < s.Length; i++)
{
// Check if s[i] is vowel
if (isvowel(s[i]))
{
vowels += s[i];
}
// Else s[i] is consonant
else
{
consonants += s[i];
}
}
// concatenate the Strings formed
ans = consonants + vowels;
// Pointer variables
int p1 = 0;
int p2 = consonants.Length;
// Counter variable
int cnt = 0;
// Condition to check if the
// given String has only
// consonants
if (p2 == ans.Length)
{
Console.Write(0 + "\n");
return;
}
// Condition to check if the
// String has only vowels
if (ans.Length == vowels.Length)
{
Console.Write(0 + "\n");
return;
}
// Loop to find the count of
// number of positions moved
while (p2 < ans.Length)
{
if (ans[p2] == s[p1])
{
cnt += p2 - p1;
p1++;
p2++;
}
else
{
p1++;
}
}
Console.Write(cnt + "\n");
return;
}
// Driver Code
public static void Main(String[] args)
{
// Given String
String s = "abcdefghi";
// Function Call
movetofront(s);
}
}
// This code is contributed by sapnasingh4991
Javascript
9
时间复杂度: O(N),其中 N 是字符串的长度。
辅助空间: O(N),其中 N 是字符串的长度。
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live