📌  相关文章
📜  重新排列字符串以最大化任何一对元音之间的最小距离

📅  最后修改于: 2021-09-04 07:48:04             🧑  作者: Mango

给定一个字符串str ,任务是重新排列给定字符串的字符,使得任何一对元音之间的最小距离是最大的。

例子:

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

  • 遍历字符串的字符和计数元音和存在于字符串str辅音的数量,表示分别是N vN c
  • 现在,使用以下公式计算每对元音之间可以放置的最大辅音数:
  • 现在,通过放置所有元音,然后在元音对的每个相邻元音之间插入M 个辅音来构建结果字符串。
  • 构造结果字符串,如果仍有任何辅音,则只需将它们插入字符串的末尾即可。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to rearrange the string
// such that the minimum distance
// between any of vowels is maximum.
string solution(string s)
{
 
    // Store vowels and consonants
    vector vowel, consonant;
 
    // Iterate over the characters
    // of string
    for (auto i : s) {
 
        // If current character is a vowel
        if (i == 'a' || i == 'e'
            || i == 'i' || i == 'o'
            || i == 'u') {
 
            vowel.push_back(i);
        }
 
        // If current character is
        // a consonant
        else {
 
            consonant.push_back(i);
        }
    }
 
    // Stores count of vowels and
    // consonants respectively
    int Nc, Nv;
    Nv = vowel.size();
    Nc = consonant.size();
 
    int M = Nc / (Nv - 1);
 
    // Stores the resultant string
    string ans = "";
 
    // Stores count of consonants
    // appended into ans
    int consotnant_till = 0;
 
    for (auto i : vowel) {
 
        // Append vowel to ans
        ans += i;
        int temp = 0;
 
        // Append consonants
        for (int j = consotnant_till;
             j < min(Nc, consotnant_till + M);
             j++) {
 
            // Append consonant to ans
            ans += consonant[j];
 
            // Update temp
            temp++;
        }
 
        // Remove the taken
        // elements of consonant
        consotnant_till += temp;
    }
 
    // Return final ans
    return ans;
}
 
// Driver Code
int main()
{
    string str = "aaaabbbcc";
 
    // Function Call
    cout << solution(str);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to rearrange the String
// such that the minimum distance
// between any of vowels is maximum.
static String solution(String s)
{
 
    // Store vowels and consonants
    Vector vowel = new Vector();
    Vector consonant = new Vector();
 
    // Iterate over the characters
    // of String
    for (char i : s.toCharArray())
    {
 
        // If current character is a vowel
        if (i == 'a' || i == 'e'
            || i == 'i' || i == 'o'
            || i == 'u')
        {
 
            vowel.add(i);
        }
 
        // If current character is
        // a consonant
        else
        {
 
            consonant.add(i);
        }
    }
 
    // Stores count of vowels and
    // consonants respectively
    int Nc, Nv;
    Nv = vowel.size();
    Nc = consonant.size();
    int M = Nc / (Nv - 1);
 
    // Stores the resultant String
    String ans = "";
 
    // Stores count of consonants
    // appended into ans
    int consotnant_till = 0;
    for (char i : vowel)
    {
 
        // Append vowel to ans
        ans += i;
        int temp = 0;
 
        // Append consonants
        for (int j = consotnant_till;
             j < Math.min(Nc, consotnant_till + M);
             j++) {
 
            // Append consonant to ans
            ans += consonant.get(j);
 
            // Update temp
            temp++;
        }
 
        // Remove the taken
        // elements of consonant
        consotnant_till += temp;
    }
 
    // Return final ans
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    String str = "aaaabbbcc";
 
    // Function Call
    System.out.print(solution(str));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python Program for the above approach
 
# Function to rearrange the string
# such that the minimum distance
# between any of vowels is maximum.
def solution(S):
   
    # store vowels and consonants
    vowels = []
    consonants = []
     
    # Iterate over the
    # characters of string
    for i in S:
       
        # if current character is a vowel
        if (i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u'):
            vowels.append(i)
             
        # if current character is consonant
        else:
            consonants.append(i)
             
    # store count of vowels and
    # consonants respectively
    Nc = len(consonants)
    Nv = len(vowels)
    M = Nc // (Nv - 1)
 
    # store the resultant string
    ans = ""
 
    # store count of consonants
    # append into ans
    consonant_till = 0
      
    for i in vowels:
        # Append vowel to ans
        ans += i
        temp = 0
 
        # Append consonants
        for j in range(consonant_till, min(Nc, consonant_till + M)):
           
            # Appendconsonant to ans
            ans += consonants[j]
             
            # update temp
            temp += 1
             
        # Remove the taken
        # elements of consonant
        consonant_till += temp
     
    # return final answer
    return ans
 
# Driver code
S = "aaaabbbcc"
print(solution(S))
 
# This code is contributed by Virusbuddah


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
 
// Function to rearrange the String
// such that the minimum distance
// between any of vowels is maximum.
static String solution(string s)
{
 
    // Store vowels and consonants
    List vowel = new List();
    List consonant = new List();
 
    // Iterate over the characters
    // of String
    foreach (char i in s.ToCharArray())
    {
 
        // If current character is a vowel
        if (i == 'a' || i == 'e'
            || i == 'i' || i == 'o'
            || i == 'u')
        {
 
            vowel.Add(i);
        }
 
        // If current character is
        // a consonant
        else
        {
 
            consonant.Add(i);
        }
    }
 
    // Stores count of vowels and
    // consonants respectively
    int Nc, Nv;
    Nv = vowel.Count;
    Nc = consonant.Count;
    int M = Nc / (Nv - 1);
 
    // Stores the resultant String
    string ans = "";
 
    // Stores count of consonants
    // appended into ans
    int consotnant_till = 0;
    foreach (char i in vowel)
    {
 
        // Append vowel to ans
        ans += i;
        int temp = 0;
 
        // Append consonants
        for (int j = consotnant_till;
             j < Math.Min(Nc, consotnant_till + M);
             j++) {
 
            // Append consonant to ans
            ans += consonant[j];
 
            // Update temp
            temp++;
        }
 
        // Remove the taken
        // elements of consonant
        consotnant_till += temp;
    }
 
    // Return final ans
    return ans;
}
 
// Driver Code
static public void Main()
{
    String str = "aaaabbbcc";
 
    // Function Call
    Console.WriteLine(solution(str));
    }
}
 
// This code is contributed by sanjoy_62.


Javascript


输出:
abababac

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

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