Arrangement of words without changing the relative position of vowel and consonants?

In this problem, we need to find the number of ways to arrange letters in a string while maintaining the relative positions of vowels and consonants. This means vowels stay in vowel positions and consonants stay in consonant positions.

The approach involves counting vowels and consonants separately, calculating permutations for each group considering duplicate letters, then multiplying the results.

Syntax

long long arrangeWayCount(char str[]);

Algorithm

arrangeWayCount(str)

Begin
   define an array 'freq' to store frequency.
   count and place frequency of each characters in freq array. such that freq['0'] will hold
   frequency of letter 'a', freq[1] will hold frequency of 'b' and so on.
   v := number of vowels, and c := number of consonants in str
   vArrange := factorial of v
   for each vowel v in [a, e, i, o, u], do
      vArrange := vArrange / factorial of the frequency of v
   done
   cArrange := factorial of c
   for each consonant con, do
      cArrange := cArrange / factorial of the frequency of con
   done
   return vArrange * cArrange
End

Example

Here's a complete C program to solve this problem −

#include <stdio.h>
#include <string.h>

long long factorial(int n) {
    if (n == 0 || n == 1)
        return 1;
    return n * factorial(n - 1);
}

long long arrangeWayCount(char str[]) {
    long long freq[26] = {0}; /* fill frequency array to 0 */
    int v = 0, c = 0;
    int len = strlen(str);
    
    for (int i = 0; i < len; i++) {
        freq[str[i] - 'a']++;
        if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') {
            v++;
        } else {
            c++;
        }
    }
    
    long long arrangeVowel = factorial(v);
    arrangeVowel /= factorial(freq[0]);  /* vowel a */
    arrangeVowel /= factorial(freq[4]);  /* vowel e */
    arrangeVowel /= factorial(freq[8]);  /* vowel i */
    arrangeVowel /= factorial(freq[14]); /* vowel o */
    arrangeVowel /= factorial(freq[20]); /* vowel u */
    
    long long arrangeConsonant = factorial(c);
    for (int i = 0; i < 26; i++) {
        if (i != 0 && i != 4 && i != 8 && i != 14 && i != 20)
            arrangeConsonant /= factorial(freq[i]); /* frequency of all characters except vowels */
    }
    
    long long total = arrangeVowel * arrangeConsonant;
    return total;
}

int main() {
    char str[] = "computer";
    long long ans = arrangeWayCount(str);
    printf("Possible ways to arrange: %lld<br>", ans);
    return 0;
}

Output

Possible ways to arrange: 720

How It Works

For the string "computer":

  • Vowels: o, u, e (3 vowels) − can be arranged in 3! = 6 ways
  • Consonants: c, m, p, t, r (5 consonants) − can be arranged in 5! = 120 ways
  • Total arrangements: 6 × 120 = 720 ways

Conclusion

This approach uses the multiplication principle of combinatorics to count arrangements while preserving relative positions. The formula accounts for duplicate letters using factorial division.

Updated on: 2026-03-15T11:15:34+05:30

227 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements