2114. Maximum Number of Words Found in Sentences leetcode solution in c++

#include <iostream>
#include <vector>
#include <sstream>

using namespace std;

class Solution {
public:
    int maxNumberOfWords(string sentence, vector<string>& words) {
        int count = 0;
        istringstream iss(sentence);
        vector<string> sentenceWords;

        while (iss >> sentenceWords.emplace_back()) { }

        for (const string& word : words) {
            auto it = find(sentenceWords.begin(), sentenceWords.end(), word);
            if (it != sentenceWords.end()) {
                sentenceWords.erase(it);
                count++;
            } else {
                break;
            }
        }

        return count;
    }
};

int main() {
    Solution solution;
    string sentence = "the quick brown fox jumped over the lazy dog";
    vector<string> words = {"the", "quick", "brown", "fox"};

    int result = solution.maxNumberOfWords(sentence, words);
    cout << "Maximum number of words found: " << result << endl;

    return 0;
}