# Count anagrams having first character as a consonant and no pair of consonants or vowels placed adjacently

Anagrams are a fascinating concept in computer science and language processing. They are essentially words or phrases made by rearranging the letters of another word or phrase. The challenge increases when we introduce specific rules. Today, we'll delve into a unique problem - counting anagrams that start with a consonant and have no adjacent consonants or vowels. We'll use C++ to develop a solution and walk through an illustrative example.

## Algorithm Explanation

Our task is to count anagrams under two constraints −

• The first character must be a consonant.

• There should be no adjacent consonants or vowels.

To implement this, we will −

• Extract consonants and vowels separately.

• Implement a recursive function to generate combinations adhering to our rules.

## C++ Implementation

Let's explore a step-by-step C++ implementation −

### Example

Let's explore a step-by-step C++ implementation −

#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<string> anagrams;

// Function to check if a character is vowel
bool isVowel(char c) {
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}

// Function to generate anagrams following rules
void generateAnagrams(string s, string consonants, string vowels, int n) {
// Base case: when string length reaches n
if (s.length() == n) {
anagrams.push_back(s);
return;
}

// Add consonants and vowels alternatively
if (s.length() % 2 == 0) { // Add consonant
for (int i = 0; i < consonants.length(); i++) {
generateAnagrams(s + consonants[i], consonants.substr(0, i) + consonants.substr(i + 1), vowels, n);
}
} else { // Add vowel
for (int i = 0; i < vowels.length(); i++) {
generateAnagrams(s + vowels[i], consonants, vowels.substr(0, i) + vowels.substr(i + 1), n);
}
}
}

int main() {
string str;
str="abc";

string consonants = "", vowels = "";
for (int i = 0; i < str.length(); i++) {
if (isVowel(str[i])) vowels += str[i];
else consonants += str[i];
}

generateAnagrams("", consonants, vowels, str.length());

cout << "Anagrams count: " << anagrams.size() << endl;

return 0;
}


### Output

When you run the program with "abc", the output will be −

Anagrams count: 2


This program takes an input string and separates the consonants and vowels. It then recursively generates anagrams by adding consonants and vowels alternatively.

## Test Case Example

Suppose we run the program with the input string "abc". This string has two consonants ("b" and "c") and one vowel ("a"). The possible anagrams following the rules would be "bca" and "cab".

## Conclusion

In this tutorial, we've learned how to count anagrams with specific rules in C++. We discussed an approach involving separating consonants and vowels and using recursion to generate the anagrams. This not only deepens our understanding of anagrams but also expands our knowledge of how to handle constraints in programming problems.

Updated on: 17-May-2023

54 Views

##### Kickstart Your Career

Get certified by completing the course