📌  相关文章
📜  移动所有元音所需的最小交换发生在给定字符串的辅音之后

📅  最后修改于: 2021-09-06 06:08:39             🧑  作者: Mango

给定一个字符串S ,任务是计算元音必须移动的位置数,以便所有辅音都放在前面,所有元音放在最后。新字符串中辅音和元音的顺序必须相同。例子:

方法:

  1. 创建空字符串元音辅音来存储给定字符串的元音和辅音。
  2. 遍历给定的字符串S ,如果当前字符是元音,则将其附加到字符串元音字符串,否则附加到字符串辅音字符串。
  3. 辅音元音字符串的串联存储在ans 字符串。
  4. 初始化 2 个指针p1p2 ,使得 p1 指向 S 的第一个索引,而 p2 指向第一个元音出现在ans字符串的索引。
  5. 将计数器变量cnt初始化为 0。
  6. 每次在与指数P2字符索引P1相符的字符,加上P2的值– P1CNT和递增1 P1和P2的值。
  7. 对每个索引重复步骤 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