Indexed API Field Pattern Queries
Problem statement
You receive encoded API records in records and must answer the field-pattern checks in queries. Preprocess the records once, then return one boolean for each query in input order.
Record encoding
Each record contains one or more semicolon-separated key=value fields. Keys are unique within a record. A field value is considered received under its key even if the same pair appears in several records.
Query encoding
Each query also has the form key=pattern:
- If
patternhas no*, it matches only the exact value. - If
patternends in*, it matches every value that starts with the prefix before*. - The pattern
*matches any value stored under that key.
A query is true when at least one parsed record contains the requested key with a matching value. A missing key or a pattern with no matching value is false. Comparisons are case-sensitive.
Build an index over the parsed fields so that each query examines only the values stored under its requested key instead of rescanning all records.
Function
matchApiFieldPatterns(records: String[], queries: String[]) → boolean[]Examples
Example 1
records = ["method=get;path=users","method=post;path=payments","method=get;path=user_settings"]queries = ["method=get","path=pay*","path=user*","method=put","path=*"]return = [true,true,true,false,true]The exact query method=get succeeds. The path prefixes pay and user match received values, while no record contains method=put. The bare wildcard matches any stored path.
Example 2
records = ["event=charge;id=abc123","event=chargeback;id=abd900","event=refund;id=abc"]queries = ["event=charge","event=charge*","id=abc","id=abc*","id=ab*","missing=*"]return = [true,true,true,true,true,false]An exact pattern distinguishes charge from chargeback, while charge* accepts both. The exact value abc exists, and both prefix checks have matching identifiers. No parsed record contains the key missing.
Example 3
records = ["type=ping;region=us_east","region=eu;type=pong","type=ping;region=us_west"]queries = ["region=us_*","type=pong","region=e","type=*"]return = [true,true,false,true]Field order inside a record does not matter. The prefix us_ matches two regions, the exact value pong exists, and exact e does not equal eu. At least one value exists for type=*.
Constraints
0 <= records.length <= 50000.0 <= queries.length <= 50000.- Each record contains from
1through20unique fields. - Every key and value is nonempty and contains only lowercase ASCII letters, digits, and underscores.
- Each query contains one existing or absent key, one
=, and a nonempty pattern. The only allowed wildcard is one optional final*. - The total number of characters across
recordsandqueriesis at most500000. - All record and query strings follow the stated encodings.