Beer And Strings

Beer found a list of strings recently. A game came into his mind instantaneously. The game is as awkward as beer's name :D The game is how many of the strings can be made using characters of Beer's all-time favorite string X

Example:

Input:  s = "abcd", n = 2, arr = {"ab", "ef"}
Output: 1

Approach

C++

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

int beerAndString(string sint nvector<string&arr)
{
    int f[26] = {0};
    for (int i = 0i < s.size(); i++)
        f[s[i] - 'a']++;

    int cnt = 0;

    for (int i = 0i < ni++)
    {
        string str = arr[i];

        int f1[26] = {0};
        int flag = 0;
        for (int i = 0i < str.size(); i++)
        {
            if (str[i] >= 'a' && str[i] <= 'z')
                f1[str[i] - 'a']++;
            else
            {
                flag = 1;
                break;
            }
        }
        if (flag == 0)
        {
            for (int i = 0i < 26i++)
            {
                if (f1[i] > 0)
                {
                    if (f1[i] > f[i])
                    {
                        flag = 1;
                        break;
                    }
                }
            }
            if (flag == 0)
                cnt++;
        }
    }
    return cnt;
}
int main()
{
    string s = "abcd";
    int n = 2;
    vector<stringarr = {"ab""ef"};

    cout << beerAndString(snarr<< "\n";

    return 0;
}


No comments:

Post a Comment