Tagging System Counts
MediumHash Tables · Atlassian
You are building the tagging feature of an issue tracker. Process operations in order. Each operation is a list of strings in one of three forms:
["TAG", item, tag]: attachtagtoitem. Attaching a tag the item already has changes nothing.["UNTAG", item, tag]: removetagfromitemif it has it; otherwise do nothing.["QUERY", tag_1, tag_2, ...]: count how many distinct items currently carry every listed tag. A query lists at least one tag, and repeating a tag in a query has no extra effect.
Tags are case-insensitive: "Bug", "bug" and "BUG" are the same tag. Item ids are case-sensitive: "p1" and "P1" are different items.
Return a list containing the answer to each QUERY, in the order the queries appear.
Example 1
- Input:
- operations = [["TAG", "PROJ-1", "Backend"], ["TAG", "PROJ-2", "backend"], ["TAG", "PROJ-2", "Urgent"], ["QUERY", "BACKEND"], ["QUERY", "backend", "urgent"], ["UNTAG", "PROJ-2", "URGENT"], ["QUERY", "urgent"]]
- Output:
- [2, 1, 0]
- Explanation
- Both items carry
backendin some casing, so the first query counts 2. Only PROJ-2 also hasurgent, giving 1. After the untag, which also ignores case, nothing carriesurgent.
Example 2
- Input:
- operations = [["TAG", "a", "x"], ["TAG", "A", "X"], ["TAG", "a", "X"], ["QUERY", "x", "X"]]
- Output:
- [2]
- Explanation
aandAare different items and both carry tagx. Taggingaa second time changes nothing, and the repeated tag in the query counts once, so the answer is 2.
Example 3
- Input:
- operations = [["UNTAG", "p", "ops"], ["QUERY", "ops"]]
- Output:
- [0]
- Explanation
- Removing a tag the item never had does nothing, and no item carries
ops.
Constraints
0 <= operations.length <= 10^5- Each query lists between
1and5tags. - Item ids and tags are non-empty strings of letters, digits and dashes.