Count mentions per Users || Java Solution
IntuitionThe problem involves tracking mentions in a chat system, where users can mention others in messages and also have the concept of being online or offlin
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem involves tracking mentions in a chat system, where users can mention others in messages and also have the concept of being online or offline. We need to efficiently count the number of times each user is mentioned after a series of events.

# Approach
<!-- Describe your approach to solving the problem. -->
1. **Sorting events by time:** Since the events can occur out of order, sorting them will help us process them chronologically.
2. **Tracking mentions:** 
    - If a user sends a message with "ALL", all users are mentioned.
    - If a message is sent with "HERE", we count mentions only for users who are online
    - For direct mentions, we increment the counts for specific users.
3. **Offline users:**  If a user goes offline, we store the time of their last activity and consider them offline if the event occurs within 60 seconds of their last activity.

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
The Time Complexity is $$O(ElogE +E*U)$$, where E is number of events and U is number of users.

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
The Space Complexity is $$O(E+U)$$, where E is the number of events and U is the number of users.

# Code
```java []
class Solution {
    public int[] countMentions(int numberOfUsers, List<List<String>> events) {
        int[] mentions = new int[numberOfUsers];
        Map<String, Integer> offline = new HashMap<>();
        int total = 0;

        Collections.sort(events, (a, b) -> {
            int x = Integer.parseInt(a.get(1));
            int y = Integer.parseInt(b.get(1));
            if (x == y) {
                return b.get(0).compareTo(a.get(0));
            }
            return x - y;
        });

        for (List<String> event : events) {
            String action = event.get(2);
            int time = Integer.parseInt(event.get(1));

            if (event.get(0).equals("MESSAGE")) {
                if (action.equals("ALL")) {
                    total++;
                } else if (action.equals("HERE")) {
                    total++;
                    for (Map.Entry<String, Integer> entry : offline.entrySet()) {
                        int id = Integer.parseInt(entry.getKey());
                        if (time - entry.getValue() < 60) {
                            mentions[id]--;
                        }
                    }
                } else {
                    String[] mentionedUsers = action.split(" ");
                    for (String mention : mentionedUsers) {
                        mentions[Integer.parseInt(mention.substring(2))]++;
                    }
                }
            } else {
                offline.put(event.get(2), time);
            }
        }

        for (int i = 0; i < numberOfUsers; i++) {
            mentions[i] += total;
        }

        return mentions;
    }
}
```
['Java']