Prefix matching · Easy
A name-filter preview receives a list of lowercase alphanumeric identifiers and a single search prefix. It needs the number of matching entries. Duplicate identifiers represent separate records and must not be collapsed.
Return how many entries in words begin with prefix, counting duplicates as separate matching records.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
A JSON object { "words": string[], "prefix": string }.
Fields
- words contains lowercase ASCII letters a-z and digits 0-9; each entry may be empty.
- prefix uses the same alphabet and may be empty.
Output
Return value
- Return an integer count, not the matching words.
- An empty prefix matches every entry, including empty words.
- This is one prefix query; building a trie is optional and earns no extra credit.
Constraints
- 0 <= words.length <= 100000; total word characters <= 1000000; prefix.length <= 1000.
- Digits are explicitly allowed because the original published test data already uses identifiers such as app10.
Examples
Example 1
{
"prefix": "app",
"words": [
"app10",
"api3",
"app10",
"cache"
]
}Returns
2Both app10 records count.
Example 2
{
"prefix": "",
"words": [
"",
"a"
]
}Returns
2Every entry has an empty prefix.
Required complexity
O(total word characters + prefix.length) time or better; constant working space is possible