FastPrepCount Group Chat Mentions

Count Group Chat Mentions

TikTok logoTikTok● MediumNEW GRADOA

Problem statement

You are given the group member IDs in members and the chat text in messages. Count how many messages mention each member.

A mention is a space-separated token that starts with @, followed by one or more IDs separated by commas, such as @id1,id123,id983. Every ID is id followed by an integer from 1 through 999, written without leading zeros. Mention tokens appear at the beginning or end of a message, or have an ASCII space on each side. Every token starting with @ is a valid mention token. An @ inside an ordinary word, such as appro@ch, is not a mention.

Count a member at most once per message, even if that ID appears repeatedly in one token or in several mention tokens. Ignore mentioned IDs that are absent from members. Include members with zero mentions.

Return one string per member in the form id=count. Sort by count in descending order, then by the complete ID string in lexicographic ascending order. For example, id10 comes before id2 when counts tie.

Function

mentionCounts(members: String[], messages: String[]) → String[]

Examples

Example 1

members = ["id123","id234","id7","id321"]messages = ["Hey @id123,id321 review this PR please! @id123 what do you think about this approach?","Hey @id7 nice appro@ch! Great job! @id800 what do you think?","@id123,id321 thx!"]return = ["id123=2","id321=2","id7=1","id234=0"]

The first and third messages mention id123 and id321; repeated id123 in the first message counts once. The second message mentions member id7 and nonmember id800. The @ in appro@ch is not a mention. Member id234 is never mentioned. The tied count-two members are ordered as id123, then id321.

Example 2

members = ["id2","id10","id1"]messages = ["@id2,id10","hello"]return = ["id10=1","id2=1","id1=0"]

The first message mentions id2 and id10 once each; the second mentions nobody. For the tied count one, lexicographic ordering puts id10 before id2.

Constraints

  • 1 <= members.length <= 999
  • 0 <= messages.length <= 1000
  • 3 <= members[i].length <= 5
  • 1 <= messages[i].length <= 1000
  • Members are distinct canonical IDs from id1 through id999.
  • Each message contains only printable ASCII characters. Tokens are separated by ASCII spaces; multiple spaces are allowed. Tokens starting with @ follow the valid mention syntax described above.

More TikTok problems

See TikTok hiring insights
Saved
public String[] mentionCounts(String[] members, String[] messages) {
    // Return sorted member=count strings.
}
members["id123","id234","id7","id321"]
messages["Hey @id123,id321 review this PR please! @id123 what do you think about this approach?","Hey @id7 nice appro@ch! Great job! @id800 what do you think?","@id123,id321 thx!"]
expected["id123=2", "id321=2", "id7=1", "id234=0"]
Checking account…