Arrangement of the characters of a word such that all vowels are at odd places - GeeksforGeeks

07 May, 2021
Given a string ‘S’ containing vowels and consonants of lowercase English alphabets. The task is 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.
Examples: 
Input: geeks Output: 36 Input: publish Output: 1440  
Approach:  
First find the total no. of odd places and even places in the given word.Total number of even places = floor(word length/2) Total number of odd places = word length – total even placesLet’s consider the string “contribute” then there are 10 letters in the given word and there are 5 odd places, 5 even places, 4 vowels and 6 consonants.Let us mark these positions as under: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)Now, 4 vowels can be placed at any of the five places, marked 1, 3, 5, 7, 9. The number of ways of arranging the vowels = 5_P_4 = 5! = 120Also, the 6 consonants can be arranged at the remaining 6 positions. Number of ways of these arrangements = 6_P_6 = 6! = 720.Total number of ways = (120 x 720) = 86400  
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// 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 <bits/stdc++.h>using namespace std; // Function to return the// factorial of a numberint fact(int n){    int f = 1;    for (int i = 2; i <= n; i++) {        f = f * i;    }     return f;} // calculating nPrint 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 positionsint 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 codeint main(){    string str = "geeks";     cout << countPermutations(str);     return 0;}
// 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 positionsclass GFG{// Function to return the// factorial of a numberstatic int fact(int n){    int f = 1;    for (int i = 2; i <= n; i++) {        f = f * i;    }     return f;} // calculating nPrstatic 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 positionsstatic 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 codepublic static void main(String[] args){    String str = "geeks";     System.out.println(countPermutations(str));}}// This code is contributed by mits
# 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 positionsimport math # Function to return the factorial# of a numberdef fact(n):    f = 1;    for i in range(2, n + 1):        f = f * i;     return f; # calculating nPrdef 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 positionsdef 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 codestr = "geeks"; print(countPermutations(str));     # This code is contributed by mits
// 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 positionsusing System;class GFG{// Function to return the// factorial of a numberstatic int fact(int n){    int f = 1;    for (int i = 2; i <= n; i++) {        f = f * i;    }     return f;} // calculating nPrstatic 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 positionsstatic 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 codestatic void Main(){    String str = "geeks";     Console.WriteLine(countPermutations(str));}}// This code is contributed by mits
<?php// PHP 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 // Function to return the// factorial of a numberfunction fact($n){    $f = 1;    for ($i = 2; $i <= $n; $i++)    {        $f = $f * $i;    }     return $f;} // calculating nPrfunction 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 positionsfunction countPermutations($str){    // Get total even positions    $even = floor(strlen($str)/ 2);     // Get total odd positions    $odd = strlen($str) - $even;     $ways = 0;     // Store $frequency of each    // character of the string    $freq = array_fill(0, 26, 0);    for ($i = 0; $i < strlen($str); $i++)    {        ++$freq[ord($str[$i]) - ord('a')];    }     // Count total number of vowels    $nvowels= $freq[0] + $freq[4] +              $freq[8] + $freq[14] +              $freq[20];     // Count total number of consonants    $nconsonants= strlen($str) - $nvowels;     // Calculate the total number of ways    $ways = npr($odd, $nvowels) *            npr($nconsonants, $nconsonants);     return $ways;} // Driver code$str = "geeks"; echo countPermutations($str);     // This code is contributed by mits?>
<script>// Javascript 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 // Function to return the// factorial of a numberfunction fact(n){    let f = 1;    for (let i = 2; i <= n; i++)    {        f = f * i;    }     return f;} // calculating nPrfunction 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 positionsfunction countPermutations(str){    // Get total even positions    let even = Math.floor(str.length/ 2);     // Get total odd positions    let odd = str.length - even;     let ways = 0;     // Store frequency of each    // character of the string    let freq = new Array(26).fill(0);    for (let i = 0; i < str.length; i++)    {        ++freq[str.charCodeAt(i) - 'a'.charCodeAt(0)];    }     // Count total number of vowels    let nvowels= freq[0] + freq[4] +            freq[8] + freq[14] +            freq[20];     // Count total number of consonants    let nconsonants= str.length - nvowels;     // Calculate the total number of ways    ways = npr(odd, nvowels) *            npr(nconsonants, nconsonants);     return ways;} // Driver codelet str = "geeks"; document.write(countPermutations(str));     // This code is contributed by gfgking</script>
36
 
Mithun Kumar
gfgking
factorial
permutation
vowel-consonant
Mathematical
Strings
Strings
Mathematical
permutation
factorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Modular multiplicative inverse
Count all possible paths from top left to bottom right of a mXn matrix
Fizz Buzz Implementation
Write a program to reverse an array or string
Reverse a string in Java
Longest Common Subsequence | DP-4
Check for Balanced Brackets in an expression (well-formedness) using Stack
Python program to check if a string is palindrome or not