in ,

Count occurrences of strings formed using words in another string

  

#include

using namespace std;

  

vector getWords(string A)

{

    vector words;

    string t;

    for (int i = 0; i < A.size(); i++) {

  

        

        if (A[i] == ' ') {

            if (t.size() > 0) {

                words.push_back(t);

            }

            t = "";

        }

  

        

        else {

            t += A[i];

        }

    }

  

    

    if (t.size() > 0) {

        words.push_back(t);

    }

  

    return words;

}

  

int countStrings(string A, vector& B)

{

  

    unordered_set st;

  

    vector words;

    words = getWords(A);

  

    for (auto x : words) {

        st.insert(x);

    }

  

    

    int ans = 0;

    for (auto x : B) {

        words = getWords(x);

        bool flag = 0;

        for (auto y : words) {

            if (st.find(y) == st.end()) {

                flag = 1;

                break;

            }

        }

  

        

        if (!flag) {

            ans++;

        }

    }

  

    return ans;

}

  

int main()

{

    string A = "blue green red yellow";

    vector B = { "blue red", "green pink", "yellow green" };

  

    cout << countStrings(A, B);

}

What do you think?

Silver 1

Written by yulica

Leave a Reply

Your email address will not be published.

      Minimize operations to convert (0, 0) to (N, M) by incrementing either or both by K

      SCAND Is Celebrating Its 20th Anniversary_61d8c28bb0417.jpeg

      SCAND Is Celebrating Its 20th Anniversary