Beginner friendly ✅ | C++ | Beginner friendly💯
Complexity
Time complexity:O(m X n)

Space complexity:O(n)
Code


# Complexity
- Time complexity:O(m X n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:O(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
vector<int> countMentions(int numberOfUsers, vector<vector<string>>& events) {
 int n = numberOfUsers;
    vector<int> mentions(n, 0);       // Mention count for each user
    vector<int> offlineUntil(n, 0);   // Track the timestamp until each user is offline
    unordered_set<int> onlineUsers;   // Track currently online users

    // Initially, all users are online
    for (int i = 0; i < n; ++i) {
        onlineUsers.insert(i);
    }

    // Sort events by timestamp. If timestamps are the same, prioritize OFFLINE events.
    sort(events.begin(), events.end(), [](const vector<string>& a, const vector<string>& b) {
        if (stoi(a[1]) == stoi(b[1])) {
            return a[0] == "OFFLINE"; // Prioritize OFFLINE events if timestamps are the same
        }
        return stoi(a[1]) < stoi(b[1]); // Sort by timestamp
    });

    for (const auto& event : events) {
        string eventType = event[0];
        int timestamp = stoi(event[1]);

        // Process OFFLINE events first to update the online status of users before MESSAGE events
        if (eventType == "OFFLINE") {
            // Extract userId from the event
            int userId = stoi(event[2]);
            offlineUntil[userId] = timestamp + 60; // User goes offline for 60 minutes
            onlineUsers.erase(userId); // Remove user from online users
        }

        // After processing OFFLINE events, now process MESSAGE events
        if (eventType == "MESSAGE") {
            string mentionsString = event[2];

            // Update the online status for all users at the current timestamp
            for (int i = 0; i < n; ++i) {
                // If the user was offline until this timestamp, they should be online now
                if (offlineUntil[i] <= timestamp && onlineUsers.find(i) == onlineUsers.end()) {
                    onlineUsers.insert(i);
                }
            }

            if (mentionsString == "ALL") {
                // Mention all users
                for (int i = 0; i < n; ++i) {
                    mentions[i]++;
                }
            } else if (mentionsString == "HERE") {
                // Mention all currently online users
                for (int user : onlineUsers) {
                    mentions[user]++;
                }
            } else {
                // Process individual user mentions
                stringstream ss(mentionsString);
                string token;
                while (ss >> token) {
                    if (token.substr(0, 2) == "id") {
                        int userId = stoi(token.substr(2));  // Extract the user ID after "id"
                        mentions[userId]++;
                    }
                }
            }
        }

        // Update online status at the end of each event to check if any user should come back online
        for (int i = 0; i < n; ++i) {
            if (offlineUntil[i] <= timestamp && onlineUsers.find(i) == onlineUsers.end()) {
                onlineUsers.insert(i); // Bring the user back online if the offline time has passed
            }
        }
    }

    return mentions;
}


};
```
['Hash Table', 'String', 'Sorting']