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Ā 
3_P_2 \times 3_P_3 = 36

Input: publishĀ 
Output: 1440Ā 
4_P_2 \times 5_P_5 = 720      Ā 

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 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 = "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 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 = "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 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 = "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 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 = "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 number
function fact($n)
{
Ā Ā Ā Ā $f = 1;
Ā Ā Ā Ā for ($i = 2; $i <= $n; $i++)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā $f = $f * $i;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā return $f;
}
Ā 
// calculating nPr
function 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
function 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 number
function fact(n)
{
Ā Ā Ā Ā let f = 1;
Ā Ā Ā Ā for (let i = 2; i <= n; i++)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā f = f * i;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā return f;
}
Ā 
// calculating nPr
function 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
function 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 code
let 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!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button