Monday, February 22, 2021

Published February 22, 2021 by Anonymous with 0 comment

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

#include <bits/stdc++.h>

  

#define ll long long

#define mod 1000000007

#define N 1000001

using namespace std;

  

void Precomputefact(unordered_map<ll, ll>& fac)

{

    ll ans = 1;

  

    

    for (ll i = 1; i <= N; i++) {

  

        

        ans = (ans * i) % mod;

  

        

        

        fac[i] = ans;

    }

    return;

}

  

bool isVowel(char a)

{

    if (a == 'A' || a == 'E' || a == 'I' || a == 'O'

        || a == 'U')

        return true;

    else

        return false;

}

  

void countAnagrams(string s, int n)

{

    

    unordered_map<ll, ll> fac;

  

    

    

    Precomputefact(fac);

  

    

    

    unordered_map<char, ll> count;

  

    

    

    int vo = 0, co = 0;

  

    

    

    for (int i = 0; i < n; i++) {

  

        

        

        count[s[i]]++;

  

        

        

        if (isVowel(s[i]))

            vo++;

        else

            co++;

    }

  

    

    if ((co == vo + 1) || (co == vo)) {

  

        

        ll deno = 1;

  

        

        

        for (auto c : count) {

  

            

            

            deno = (deno * fac[c.second]) % mod;

        }

  

        

        ll nume = fac[co] % mod;

        nume = (nume * fac[vo]) % mod;

  

        

        

        ll ans = nume / deno;

  

        

        cout << ans;

    }

  

    

    else {

        cout << 0;

    }

}

  

int main()

{

    string S = "GADO";

    int l = S.size();

    countAnagrams(S, l);

  

    return 0;

}

Let's block ads! (Why?)

      edit

0 comments:

Post a Comment