Acronym

Our friend Harsh has been recently reducing every sentence he receives to an acronym. Since Harsh is a young lad he has his own personal likes and dislikes. There are some words which he just does not want to see in a sentence. So whenever he encounters such a word he simply ignores it. Harsh is thinking of converting a book to an acronym book where each sentence of the book is reduced to its short form. Since Harsh is quite busy promoting his new book he wants you to help him in doing so.

So he gives you a set of words that he dislikes and a sentence for which he wants to abbreviate. Help him in getting the required abbreviation.

Example:

Input: k = 5, s = {"hey", "girls", "i", "am", "single"}, n = 11, str = {"hey", "all", "boys", "and", "girls", "welcome", "to", "hackerearth", "easy", "september", "challenge"}
Output: A.B.A.W.T.H.E.S.C

Approach

C++

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

string acronym(int kvector<string&s,
               int nvector<string&str)
{
    set<stringst;
    for (int i = 0i < ki++)
    {

        st.insert(s[i]);
    }

    string res = "";
    for (int i = 0i < ni++)
    {
        if (st.find(str[i]== st.end())
        {
            if (str[i][0] >= 'a' && str[i][0] <= 'z')
                res += str[i][0] - 32;
            else
                res += str[i][0];
            if (i < n - 1)
                res += '.';
        }
    }
    string ans = "";
    int len = res.size();
    for (int i = 0i < len - 1i++)
        ans += res[i];
    if (res[len - 1] != '.')
        ans += res[len - 1];
    return ans;
}
int main()
{
    int k = 5;
    vector<strings = {"hey""girls""i""am""single"};

    int n = 11;
    vector<stringstr = {"hey""all""boys",
                          "and""girls",
                          "welcome""to",
                          "hackerearth""easy",
                          "september""challenge"};

    cout << acronym(ksnstr<< "\n";
    return 0;
}


No comments:

Post a Comment