Given a string consisting of vowels and consonants. The task is to find the number of ways in which the characters of the string can be arranged such that no two vowels are adjacent to each other.
Note: Given that No. of vowels <= No. of consonants.
Examples:
Input: str = "permutation" Output : 907200 Input: str = "geeksforgeeks" Output: 3175200
Approach:
Consider the above example string “permutation”:
- First place all of the consonants at the alternate places like below:
-- p -- r -- m -- t -- t -- n --
Number of ways to place consonants = 6! / 2!. as
appears twice and should be considered once. - Then place the vowels at the remaining positions. We have 7 remaining positions and 5 vowels to fill these 7 places.
Therefore, number of ways to fill vowels =
.
Total no. of ways == 907200
Suppose in a string number of vowels is vowelCount and the number of consonants is consonantCount.
Therefore,
Total ways = (consonantCount! / duplicateConsonant!) * C(consonantCount+1 , vowelCount) * (vowelCount! / duplicateVowel!)
Below is the implementation of the above approach:
C++
// CPP program to count permutations of string // such that no two vowels are adjacent #include <bits/stdc++.h> using namespace std; // Factorial of a number int factorial(int n) { int fact = 1; for (int i = 2; i <= n; i++) fact = fact * i; return fact; } // Function to find c(n, r) int ncr(int n, int r) { return factorial(n) / (factorial(r) * factorial(n - r)); } // Function to count permutations of string // such that no two vowels are adjacent int countWays(string str) { int freq[26] = { 0 }; int nvowels = 0, nconsonants = 0; int vplaces, cways, vways; // Finding the frequencies of // the characters for (int i = 0; i < str.length(); i++) ++freq[str[i] - 'a']; // finding the no. of vowels and // consonants in given word for (int i = 0; i < 26; i++) { if (i == 0 || i == 4 || i == 8 || i == 14 || i == 20) nvowels += freq[i]; else nconsonants += freq[i]; } // finding places for the vowels vplaces = nconsonants + 1; // ways to fill consonants 6! / 2! cways = factorial(nconsonants); for (int i = 0; i < 26; i++) { if (i != 0 && i != 4 && i != 8 && i != 14 && i != 20 && freq[i] > 1) { cways = cways / factorial(freq[i]); } } // ways to put vowels 7C5 x 5! vways = ncr(vplaces, nvowels) * factorial(nvowels); for (int i = 0; i < 26; i++) { if (i == 0 || i == 4 || i == 8 || i == 14 || i == 20 && freq[i] > 1) { vways = vways / factorial(freq[i]); } } return cways * vways; } // Driver code int main() { string str = "permutation"; cout << countWays(str) << endl; return 0; } |
Java
// Java program to count permutations of string // such that no two vowels are adjacent class GFG { // Factorial of a number static int factorial(int n) { int fact = 1; for (int i = 2; i <= n; i++) fact = fact * i; return fact; } // Function to find c(n, r) static int ncr(int n, int r) { return factorial(n) / (factorial(r) * factorial(n - r)); } // Function to count permutations of string // such that no two vowels are adjacent static int countWays(String str) { int freq[]=new int[26]; for(int i=0;i<26;i++) { freq[i]=0; } int nvowels = 0, nconsonants = 0; int vplaces, cways, vways; // Finding the frequencies of // the characters for (int i = 0; i < str.length(); i++) ++freq[str.charAt(i) - 'a']; // finding the no. of vowels and // consonants in given word for (int i = 0; i < 26; i++) { if (i == 0 || i == 4 || i == 8 || i == 14 || i == 20) nvowels += freq[i]; else nconsonants += freq[i]; } // finding places for the vowels vplaces = nconsonants + 1; // ways to fill consonants 6! / 2! cways = factorial(nconsonants); for (int i = 0; i < 26; i++) { if (i != 0 && i != 4 && i != 8 && i != 14 && i != 20 && freq[i] > 1) { cways = cways / factorial(freq[i]); } } // ways to put vowels 7C5 x 5! vways = ncr(vplaces, nvowels) * factorial(nvowels); for (int i = 0; i < 26; i++) { if (i == 0 || i == 4 || i == 8 || i == 14 || i == 20 && freq[i] > 1) { vways = vways / factorial(freq[i]); } } return cways * vways; } // Driver code public static void main(String []args) { String str = "permutation"; System.out.println(countWays(str)); } } // This code is contributed // by ihritik |
Python3
# Python3 program to count permutations of # string such that no two vowels are adjacent # Factorial of a number def factorial(n) : fact = 1; for i in range(2, n + 1) : fact = fact * i return fact # Function to find c(n, r) def ncr(n, r) : return factorial(n) // (factorial(r) * factorial(n - r)) # Function to count permutations of string # such that no two vowels are adjacent def countWays(string) : freq = [0] * 26 nvowels, nconsonants = 0, 0 # Finding the frequencies of # the characters for i in range(len(string)) : freq[ord(string[i]) - ord('a')] += 1 # finding the no. of vowels and # consonants in given word for i in range(26) : if (i == 0 or i == 4 or i == 8 or i == 14 or i == 20) : nvowels += freq[i] else : nconsonants += freq[i] # finding places for the vowels vplaces = nconsonants + 1 # ways to fill consonants 6! / 2! cways = factorial(nconsonants) for i in range(26) : if (i != 0 and i != 4 and i != 8 and i != 14 and i != 20 and freq[i] > 1) : cways = cways // factorial(freq[i]) # ways to put vowels 7C5 x 5! vways = ncr(vplaces, nvowels) * factorial(nvowels) for i in range(26) : if (i == 0 or i == 4 or i == 8 or i == 14 or i == 20 and freq[i] > 1) : vways = vways // factorial(freq[i]) return cways * vways; # Driver code if __name__ == "__main__" : string = "permutation" print(countWays(string)) # This code is contributed by Ryuga |
C#
// C# program to count permutations of string // such that no two vowels are adjacent using System; class GFG { // Factorial of a number static int factorial(int n) { int fact = 1; for (int i = 2; i <= n; i++) fact = fact * i; return fact; } // Function to find c(n, r) static int ncr(int n, int r) { return factorial(n) / (factorial(r) * factorial(n - r)); } // Function to count permutations of string // such that no two vowels are adjacent static int countWays(String str) { int []freq=new int[26]; for(int i=0;i<26;i++) { freq[i]=0; } int nvowels = 0, nconsonants = 0; int vplaces, cways, vways; // Finding the frequencies of // the characters for (int i = 0; i < str.Length; i++) ++freq[str[i] - 'a']; // finding the no. of vowels and // consonants in given word for (int i = 0; i < 26; i++) { if (i == 0 || i == 4 || i == 8 || i == 14 || i == 20) nvowels += freq[i]; else nconsonants += freq[i]; } // finding places for the vowels vplaces = nconsonants + 1; // ways to fill consonants 6! / 2! cways = factorial(nconsonants); for (int i = 0; i < 26; i++) { if (i != 0 && i != 4 && i != 8 && i != 14 && i != 20 && freq[i] > 1) { cways = cways / factorial(freq[i]); } } // ways to put vowels 7C5 x 5! vways = ncr(vplaces, nvowels) * factorial(nvowels); for (int i = 0; i < 26; i++) { if (i == 0 || i == 4 || i == 8 || i == 14 || i == 20 && freq[i] > 1) { vways = vways / factorial(freq[i]); } } return cways * vways; } // Driver code public static void Main() { String str = "permutation"; Console.WriteLine(countWays(str)); } } // This code is contributed // by ihritik |
PHP
<?php // CPP program to count permutations of string // such that no two vowels are adjacent // Factorial of a number function factorial($n) { $fact = 1; for ($i = 2; $i <= $n; $i++) $fact = $fact * $i; return $fact; } // Function to find c(n, r) function ncr($n, $r) { return factorial($n) / (factorial($r) * factorial($n - $r)); } // Function to count permutations of string // such that no two vowels are adjacent function countWays($str) { $freq = array_fill(0,26,NULL); $nvowels = 0; $nconsonants = 0; // Finding the frequencies of // the characters for ($i = 0; $i < strlen($str); $i++) ++$freq[ord($str[$i]) - ord('a')]; // finding the no. of vowels and // consonants in given word for ($i = 0; $i < 26; $i++) { if ($i == 0 || $i == 4 || $i == 8 || $i == 14 || $i == 20) $nvowels += $freq[$i]; else $nconsonants += $freq[$i]; } // finding places for the vowels $vplaces = $nconsonants + 1; // ways to fill consonants 6! / 2! $cways = factorial($nconsonants); for ($i = 0; $i < 26; $i++) { if ($i != 0 && $i != 4 && $i != 8 && $i != 14 && $i != 20 && $freq[$i] > 1) { $cways = $cways / factorial($freq[$i]); } } // ways to put vowels 7C5 x 5! $vways = ncr($vplaces, $nvowels) * factorial($nvowels); for ($i = 0; $i < 26; $i++) { if ($i == 0 || $i == 4 || $i == 8 || $i == 14 || $i == 20 && $freq[$i] > 1) { $vways = $vways / factorial($freq[$i]); } } return $cways * $vways; } // Driver code $str = "permutation"; echo countWays($str)."\n"; return 0; // this code is contributed by Ita_c. ?> |
Output:
907200
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Find the number of words of X vowels and Y consonants that can be formed from M vowels and N consonants
- Longest substring of vowels with no two adjacent alphabets same
- Find the player to last modify a string such that even number of consonants and no vowels are left in the string
- Generate permutations with only adjacent swaps allowed
- Count of distinct permutations of length N having no similar adjacent characters
- Modify the string such that it contains all vowels at least once
- Ways to paint stairs with two colors such that two adjacent are not yellow
- Replace '?' in a string such that no two adjacent characters are same
- Modify string by removing vowels in between two consonants
- Number of ways to arrange a word such that all vowels occur together
- Arrangement of the characters of a word such that all vowels are at odd places
- Print an N x M matrix such that each row and column has all the vowels in it
- Number of ways to arrange a word such that no vowels occur together
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Number of permutations such that sum of elements at odd index and even index are equal
- Number of Permutations such that no Three Terms forms Increasing Subsequence
- Count of permutations such that sum of K numbers from given range is even
- Program to remove vowels from a String
- Program to count vowels in a string (Iterative and Recursive)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

