LeetCode 3433: Count Mentions Per User Solution

Master LeetCode problem 3433 (Count Mentions Per User), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3433. Count Mentions Per User

Problem Explanation

Explanation

To solve this problem, we simulate the events in chronological order and keep track of the mentions for each user. We maintain a set to represent the online users and update it when a user goes offline or comes back online. We also maintain a count of mentions for each user.

  1. Initialize a mentions array of size numberOfUsers to store the mention count for each user.
  2. Initialize an online set to store the currently online users.
  3. Iterate through the events in chronological order:
    • For each "OFFLINE" event, remove the user from the online set.
    • For each "MESSAGE" event, update the mention count for each mentioned user based on the type of mention.
      • If the mention is "ALL", increment the mention count for all online users.
      • If the mention is "HERE", increment the mention count for all users in the online set.
      • If the mention is user-specific, extract the user IDs and increment the mention count for each user.
  4. Return the mentions array as the final result.

Time Complexity: O(n) where n is the number of events. Space Complexity: O(numberOfUsers) for the mentions array and online set.

Solution Code

class Solution {
    public int[] countMentions(int numberOfUsers, String[][] events) {
        int[] mentions = new int[numberOfUsers];
        Set<Integer> online = new HashSet<>();
        
        for (String[] event : events) {
            if (event[0].equals("OFFLINE")) {
                online.remove(Integer.parseInt(event[2]));
            } else if (event[0].equals("MESSAGE")) {
                String[] mentionsString = event[2].split(" ");
                for (String mention : mentionsString) {
                    if (mention.equals("ALL")) {
                        for (int user : online) {
                            mentions[user]++;
                        }
                    } else if (mention.equals("HERE")) {
                        for (int user = 0; user < numberOfUsers; user++) {
                            if (online.contains(user)) {
                                mentions[user]++;
                            }
                        }
                    } else {
                        int id = Integer.parseInt(mention.substring(2));
                        mentions[id]++;
                    }
                }
            }
        }
        
        return mentions;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3433 (Count Mentions Per User)?

This page provides optimized solutions for LeetCode problem 3433 (Count Mentions Per User) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3433 (Count Mentions Per User)?

The time complexity for LeetCode 3433 (Count Mentions Per User) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3433 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3433 in Java, C++, or Python.

Back to LeetCode Solutions