Build a Log Inverted Index
Problem statement
Build an inverted index for the records in logs. A term is a nonempty token separated from other tokens by exactly one ASCII space. An empty log has no terms. Compare complete terms case-sensitively.
For every distinct term, collect the zero-based indices of logs containing it. Include a log index only once for that term, even when the term appears repeatedly in that log. Equal log strings at different indices remain separate records.
Return one string per term in increasing lexicographic term order. Each string is term:i,j,..., with its record indices in increasing numeric order, ordinary decimal indices and no spaces. Return an empty array if no term occurs. For example, an entry cpu:0,12 means that logs 0 and 12 contain the complete term cpu.
Function
buildLogIndex(logs: String[]) → String[]Examples
Example 1
logs = ["cpu cpu db","db cache","cpu"]return = ["cache:1","cpu:0,2","db:0,1"]Repeated cpu in record 0 contributes its index only once.
Example 2
logs = ["","a","a"]return = ["a:1,2"]The empty record retains index 0, so the equal later records keep their original indices 1 and 2.
Example 3
logs = ["Dog dog","datadog"]return = ["Dog:0","datadog:1","dog:0"]Terms match whole tokens and preserve case. ASCII lexicographic order places uppercase D before lowercase d.
Constraints
0 <= logs.length <= 2000 <= logs[i].length <= 2000- The sum of all log lengths is at most
20000. - Each term has between
1and30characters, using only ASCII letters, digits, underscores and hyphens. - A nonempty log has exactly one ASCII space between terms, with no leading or trailing space.