📌  相关文章
📜  单词的字符排列,以使所有元音都位于奇数位置

📅  最后修改于: 2021-04-29 17:37:48             🧑  作者: Mango

给定一个字符串“ S”,其中包含小写英文字母的元音和辅音。任务是找到可以排列单词字符的方式,以使元音仅占据奇数位置。

例子:

方法:

下面是上述方法的实现:

C++
// C++ program to find the number of ways
// in which the characters of the word
// can be arranged such that the vowels
// occupy only the odd positions
#include 
using namespace std;
  
// Function to return the
// factorial of a number
int fact(int n)
{
    int f = 1;
    for (int i = 2; i <= n; i++) {
        f = f * i;
    }
  
    return f;
}
  
// calculating nPr
int npr(int n, int r)
{
    return fact(n) / fact(n - r);
}
  
// Function to find the number of ways
// in which the characters of the word
// can be arranged such that the vowels
// occupy only the odd positions
int countPermutations(string str)
{
    // Get total even positions
    int even = floor(str.length() / 2);
  
    // Get total odd positions
    int odd = str.length() - even;
  
    int ways = 0;
  
    // Store frequency of each character of
    // the string
    int freq[26] = { 0 };
    for (int i = 0; i < str.length(); i++) {
        ++freq[str[i] - 'a'];
    }
  
    // Count total number of vowels
    int nvowels
        = freq[0] + freq[4]
          + freq[8] + freq[14]
          + freq[20];
  
    // Count total number of consonants
    int nconsonants
        = str.length() - nvowels;
  
    // Calculate the total number of ways
    ways = npr(odd, nvowels) * npr(nconsonants, nconsonants);
  
    return ways;
}
  
// Driver code
int main()
{
    string str = "geeks";
  
    cout << countPermutations(str);
  
    return 0;
}


Java
// Java program to find the number of ways
// in which the characters of the word
// can be arranged such that the vowels
// occupy only the odd positions
class GFG{
// Function to return the
// factorial of a number
static int fact(int n)
{
    int f = 1;
    for (int i = 2; i <= n; i++) {
        f = f * i;
    }
  
    return f;
}
  
// calculating nPr
static int npr(int n, int r)
{
    return fact(n) / fact(n - r);
}
  
// Function to find the number of ways
// in which the characters of the word
// can be arranged such that the vowels
// occupy only the odd positions
static int countPermutations(String str)
{
    // Get total even positions
    int even = (int)Math.floor((double)(str.length() / 2));
  
    // Get total odd positions
    int odd = str.length() - even;
  
    int ways = 0;
  
    // Store frequency of each character of
    // the string
    int[] freq=new int[26];
    for (int i = 0; i < str.length(); i++) {
        freq[(int)(str.charAt(i)-'a')]++;
    }
  
    // Count total number of vowels
    int nvowels= freq[0] + freq[4]+ freq[8] 
                + freq[14]+ freq[20];
  
    // Count total number of consonants
    int nconsonants= str.length() - nvowels;
  
    // Calculate the total number of ways
    ways = npr(odd, nvowels) * npr(nconsonants, nconsonants);
  
    return ways;
}
  
// Driver code
public static void main(String[] args)
{
    String str = "geeks";
  
    System.out.println(countPermutations(str));
}
}
// This code is contributed by mits


Python3
# Python3 program to find the number 
# of ways in which the characters 
# of the word can be arranged such 
# that the vowels occupy only the 
# odd positions
import math
  
# Function to return the factorial
# of a number
def fact(n):
    f = 1;
    for i in range(2, n + 1):
        f = f * i;
  
    return f;
  
# calculating nPr
def npr(n, r):
    return fact(n) / fact(n - r);
  
# Function to find the number of 
# ways in which the characters of 
# the word can be arranged such 
# that the vowels occupy only the 
# odd positions
def countPermutations(str):
  
    # Get total even positions
    even = math.floor(len(str) / 2);
  
    # Get total odd positions
    odd = len(str) - even;
  
    ways = 0;
  
    # Store frequency of each 
    # character of the string
    freq = [0] * 26;
    for i in range(len(str)):
        freq[ord(str[i]) - ord('a')] += 1;
  
    # Count total number of vowels
    nvowels = (freq[0] + freq[4] + freq[8] + 
               freq[14] + freq[20]);
  
    # Count total number of consonants
    nconsonants = len(str) - nvowels;
  
    # Calculate the total number of ways
    ways = (npr(odd, nvowels) * 
            npr(nconsonants, nconsonants));
  
    return int(ways);
  
# Driver code
str = "geeks";
  
print(countPermutations(str));
      
# This code is contributed by mits


C#
// C# program to find the number of ways
// in which the characters of the word
// can be arranged such that the vowels
// occupy only the odd positions
using System;
class GFG{
// Function to return the
// factorial of a number
static int fact(int n)
{
    int f = 1;
    for (int i = 2; i <= n; i++) {
        f = f * i;
    }
  
    return f;
}
  
// calculating nPr
static int npr(int n, int r)
{
    return fact(n) / fact(n - r);
}
  
// Function to find the number of ways
// in which the characters of the word
// can be arranged such that the vowels
// occupy only the odd positions
static int countPermutations(String str)
{
    // Get total even positions
    int even = (int)Math.Floor((double)(str.Length / 2));
  
    // Get total odd positions
    int odd = str.Length - even;
  
    int ways = 0;
  
    // Store frequency of each character of
    // the string
    int[] freq=new int[26];
    for (int i = 0; i < str.Length; i++) {
        freq[(int)(str[i]-'a')]++;
    }
  
    // Count total number of vowels
    int nvowels= freq[0] + freq[4]+ freq[8] 
                + freq[14]+ freq[20];
  
    // Count total number of consonants
    int nconsonants= str.Length - nvowels;
  
    // Calculate the total number of ways
    ways = npr(odd, nvowels) * npr(nconsonants, nconsonants);
  
    return ways;
}
  
// Driver code
static void Main()
{
    String str = "geeks";
  
    Console.WriteLine(countPermutations(str));
}
}
// This code is contributed by mits


PHP


输出:
36