Dynamic Largest Anagram Group
Problem statement
Design a collection of words that supports three operations:
["add", word]adds one occurrence ofword.["remove", word]removes one occurrence ofwordwhen it is present. Removing an absent word does nothing.["largestGroup"]reports the largest current group of mutually anagrammatic words.
You are given the operations in order as operations. Return one row for each largestGroup operation, in query order.
A group's size counts occurrences, so duplicate words are retained. Sort the words inside every returned group in lexicographic order. If several groups have the same maximum size, return the group whose sorted-letter anagram signature is lexicographically smallest. Return an empty row when the collection is empty.
Your design should make the trade-off between update work and query work explicit: workloads with frequent queries may maintain an ordered group index, while workloads with rare queries may scan the groups on demand.
Function
largestAnagramGroups(operations: String[][]) → String[][]Examples
Example 1
operations = [["add","eat"],["add","tea"],["add","tan"],["largestGroup"],["remove","eat"],["largestGroup"]]return = [["eat","tea"],["tea"]]eat and tea share one signature. After one eat is removed, the remaining group contains only tea.
Example 2
operations = [["add","ab"],["add","ba"],["add","ab"],["add","act"],["add","cat"],["largestGroup"],["remove","ab"],["largestGroup"]]return = [["ab","ab","ba"],["ab","ba"]]Occurrences are counted independently. After one copy of ab is removed, signatures ab and act both have size two, so the lexicographically smaller signature ab wins.
Example 3
operations = [["largestGroup"],["remove","ghost"],["add","listen"],["add","silent"],["largestGroup"]]return = [[],["listen","silent"]]The first query sees an empty collection. Removing an absent word is a no-op; the final two words are anagrams.
Constraints
1 <= operations.length <= 100000.- Every row is exactly
["largestGroup"],["add", word], or["remove", word]. - Every
wordcontains only lowercase English letters and has length from1through50. - The total number of characters across all operation words is at most
1000000. - The total number of words across all returned groups is at most
200000.