Count all Possible N-length Vowel Permutations that can be Generated based on the given Conditions


We are investigating the creation of N-length permutations utilising vowels in the context of this issue. The orderly arrangement of elements is referred to as a permutation. The letters A, E, I, O, and U are the vowels on which this article focuses. Finding every combination of these vowels that has a length of N is the goal. Vowels must occupy N locations in each permutation, and repetitions are permitted. Consider the permutations AAA, AAE, AIA, AOU, etc. if N is 3, for example. Calculating and compiling each of these singular permutations for a specified N are required for the assignment.

Methods Used

  • Recursion

  • Iterative Method

Recursion

In the context of N-length vowel permutations, recursion entails a self-referential procedure to produce all conceivable configurations. The programme iteratively investigates each place by adding one of the vowels (A, E, I, O, U) after starting with an empty permutation. Once it reaches the necessary length N, it continues this process by branching off to smaller subproblems. It makes sure that all plausible permutations with repeats are taken into account by going back and investigating all possibilities. It's crucial to balance flexibility and performance because recursion may be less effective for higher N due to duplicate computations.

Algorithm

  • Create a function to create permutations that takes the current permutation, the length it is currently, and the length N that is needed.

  • Check whether the current length in the function is equal to N. If so, print the latest permutation before returning.

  • Iterate over all vowels (A, E, I, O, U) if the length at the moment is less than N.

  • Add each vowel to the current permutation, then repeat the function recursively with the length incremented.

  • Remove the additional vowel from the current permutation after the recursive call to consider alternative scenarios (backtracking).

  • For each vowel, repeat steps 3 through 5, exploring every combination until you reach N-length permutations.

  • Call the permutation function with an empty permutation.

Example

#include <iostream>
using namespace std;

void generatePermutations(string current, int currentLength, int n) {
   if (currentLength == n) {
      cout << current << endl;
      return;
   }

   string vowels = "AEIOU";
   for (char vowel : vowels) {
      generatePermutations(current + vowel, currentLength + 1, n);
   }
}

int main() {
   int n = 3; 
   generatePermutations("", 0, n);
   return 0;
}

Output

AAA
AAE
AAI
AAO
AAU
AEA
AEE
AEI
AEO
AEU
AIA
AIE
AII
AIO
AIU
AOA
AOE
AOI
AOO
AOU
AUA
AUE
AUI
AUO
AUU
EAA
EAE
EAI
EAO
EAU
EEA
EEE
EEI
EEO
EEU
EIA
EIE
EII
EIO
EIU
EOA
EOE
EOI
EOO
EOU
EUA
EUE
EUI
EUO
EUU
IAA
IAE
IAI
IAO
IAU
IEA
IEE
IEI
IEO
IEU
IIA
IIE
III
IIO
IIU
IOA
IOE
IOI
IOO
IOU
IUA
IUE
IUI
IUO
IUU
OAA
OAE
OAI
OAO
OAU
OEA
OEE
OEI
OEO
OEU
OIA
OIE
OII
OIO
OIU
OOA
OOE
OOI
OOO
OOU
OUA
OUE
OUI
OUO
OUU
UAA
UAE
UAI
UAO
UAU
UEA
UEE
UEI
UEO
UEU
UIA
UIE
UII
UIO
UIU
UOA
UOE
UOI
UOO
UOU
UUA
UUE
UUI
UUO
UUU

Iterative Method

The iterative method uses nested loops to systematically create combinations in order to find all feasible N-length vowel permutations. Starting with a permutation that is empty, we fill each slot one at a time with each of the vowels (A, E, I, O, and U). We efficiently create the needed permutations by layering N loops to examine all possible combinations. This method offers a practical way to list all unique permutations without the use of recursion or complicated data structures. It is easy to build and works well for modest values of N.

Algorithm

  • Establish N, the intended length of the permutations, as a value.

  • Make a list or array to hold the permutations.

  • Create N pointers (indices) and initialise each one to 0, corresponding to a position in the permutation.

  • Make a loop that continues until every pointer reaches the last vowel index (U).

  • Construct the current permutation within the loop by joining the vowels the indices are pointing to.

  • The created permutation should be saved in an array or list.

  • Increase the rightmost pointer and determine if the length of the vowel array has been exceeded.

  • Reset a pointer to 0 and increase the pointer to its right if it exceeds the maximum index.

  • Up until all permutations are produced, repeat steps 5 through 8.

  • The array or list now includes each distinct N-length

Example

#include <iostream>
#include <string>
using namespace std;

void generatePermutations(string vowels, int N, string& permutation, int 
index) {
   if (index == N) {
      cout << permutation << endl;
      return;
   }

   for (int i = 0; i < 5; ++i) {
      permutation[index] = vowels[i];
      generatePermutations(vowels, N, permutation, index + 1);
   }
}

int main() {
   int N = 3;   string vowels = "AEIOU";
   string permutation(N, 'A');
   generatePermutations(vowels, N, permutation, 0);

   return 0;
}

Output

AAA
AAE
AAI
AAO
AAU
AEA
AEE
AEI
AEO
AEU
AIA
AIE
AII
AIO
AIU
AOA
AOE
AOI
AOO
AOU
AUA
AUE
AUI
AUO
AUU
EAA
EAE
EAI
EAO
EAU
EEA
EEE
EEI
EEO
EEU
EIA
EIE
EII
EIO
EIU
EOA
EOE
EOI
EOO
EOU
EUA
EUE
EUI
EUO
EUU
IAA
IAE
IAI
IAO
IAU
IEA
IEE
IEI
IEO
IEU
IIA
IIE
III
IIO
IIU
IOA
IOE
IOI
IOO
IOU
IUA
IUE
IUI
IUO
IUU
OAA
OAE
OAI
OAO
OAU
OEA
OEE
OEI
OEO
OEU
OIA
OIE
OII
OIO
OIU
OOA
OOE
OOI
OOO
OOU
OUA
OUE
OUI
OUO
OUU
UAA
UAE
UAI
UAO
UAU
UEA
UEE
UEI
UEO
UEU
UIA
UIE
UII
UIO
UIU
UOA
UOE
UOI
UOO
UOU
UUA
UUE
UUI
UUO
UUU

Conclusion

Using recursion and the iterative method, the examination of N-length vowel permutations offers helpful insights into the creation of all conceivable combinations of vowels with a length of N. The recursive methodology explores each location methodically using self-referential mechanisms, effectively collecting all possible permutations through repeats. However, due to redundant computations, its efficiency may drop for bigger N. For moderate numbers of N, however, the iterative technique provides a useful and effective solution by using nested loops to produce the needed permutations without the use of recursion. The particular requirements and complexity of the current challenge must be taken into consideration when selecting the best method.

Updated on: 04-Aug-2023

103 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements