Deduplicated Query Registration
Problem statement
Process query and log operations from left to right, starting with an empty registry. Split each body into words and lowercase ASCII letters. Word order does not matter.
Q: bodyregisters a query. An equivalent query reuses its existing ID; otherwise assign the next ID starting from one. ReturnRegistered qNfor this operation.L: bodymatches only queries registered so far. ReturnLog q1, q2with every matching ID in increasing numeric order, using the same comma-space separator. Return exactlyLogwhen none match.
The countWords flag selects the version:
- If false, a query is its set of distinct words. Two queries are equivalent when their word sets agree. A log matches when it contains every query word.
- If true, a query is its word-frequency map. Two queries are equivalent only when every word count agrees. A log matches when each required word appears at least as many times as the query requires. Extra words and extra repetitions are allowed.
Return exactly one output string per operation. Re-registering an equivalent query adds no duplicate match and consumes no new ID. Logs are not retained or replayed when a later query arrives. Each function call starts fresh; the mode stays fixed within that call.
Function
processDeduplicatedQueries(operations: String[], countWords: boolean) → String[]Examples
Example 1
operations = ["Q: hello world","Q: data failure","Q: world hello","L: hello world we have a data failure","L: oh no system error","Q: system error","L: oh no system error again"]countWords = truereturn = ["Registered q1","Registered q2","Registered q1","Log q1, q2","Log","Registered q3","Log q3"]The third operation reorders the first query and reuses q1. The first log contains both hello world and data failure, so it matches q1,q2. The next log matches neither. Query system error is then registered as q3, and only the final log can match it.
Example 2
operations = ["Q: a a","Q: a","Q: a a","L: a","L: a a","L: a a a"]countWords = falsereturn = ["Registered q1","Registered q1","Registered q1","Log q1","Log q1","Log q1"]Set mode reduces both queries to the set containing a. All three registrations reuse q1, and each log contains its required word at least once.
Example 3
operations = ["Q: a a","Q: a","Q: a a","L: a","L: a a","L: a a a"]countWords = truereturn = ["Registered q1","Registered q2","Registered q1","Log q2","Log q1, q2","Log q1, q2"]Frequency mode distinguishes two required copies of a from one, producing q1 and q2. The one-copy log matches only q2. Both longer logs satisfy both minimum counts, including the three-copy log.
Constraints
0 <= operations.length <= 100- Every operation has between
4and500characters. - Every operation begins with
Q:orL:, followed by at least one ASCII space and a nonempty body. The body contains1through20alphanumeric ASCII words of length1through20, separated by one or more ASCII spaces; leading and trailing body spaces are allowed. countWordsis a boolean.