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

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: zambiatekĀ
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 places
- Letā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! = 120
- Also, 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++
// 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 = "zambiatek";Ā
Ā Ā Ā Ā 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 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 = "zambiatek";Ā
Ā Ā Ā Ā 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 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 = "zambiatek";Ā
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 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 = "zambiatek";Ā
Ā Ā Ā Ā Console.WriteLine(countPermutations(str));}}// This code is contributed by mits |
PHP
<?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 = "zambiatek";Ā
echo countPermutations($str);Ā Ā Ā Ā Ā // This code is contributed by mits?> |
Javascript
<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 = "zambiatek";Ā
document.write(countPermutations(str));Ā Ā Ā Ā Ā // This code is contributed by gfgking</script> |
Output
36
Complexity Analysis:
- Time Complexity: O(n) where n is the length of the string
- Auxiliary Space: O(26)
Feeling lost in the world of random DSA topics, wasting time without progress? Itās time for a change! Join our DSA course, where weāll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



