Can you count?

You are given a string s consisting of lowercase English letters and/or '_' (underscore).

You have to replace all underscores (if any) with vowels present in the string.

The rule you follow is:
Each underscore can be replaced with any one of the vowel(s) that came before it.

You have to tell the total number of distinct strings we can generate following the above rule.

Example:

Input:  s = "ab_ae_"
Output: 2

Approach

C++

#include <bits/stdc++.h>
using namespace std;

long long count(string s)
{
    set<charst;
    long long res = 1;
    int x = st.size();
    for (int i = 0i < s.size(); i++)
    {
        if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' 
|| s[i] == 'o' || s[i] == 'u')
            st.insert(s[i]);
        else if (s[i] == '_')
        {

            res *= st.size();
            x = st.size();
        }
    }

    return res;
}
int main()
{
    string s = "ab_ae_";

    cout << count(s<< "\n";

    return 0;
}


No comments:

Post a Comment