FastPrepNumeric Wildcard Dictionary Matching

Numeric Wildcard Dictionary Matching

Headlands Technologies logoHeadlands Technologies● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

You are given a dictionary words and a query batch patterns. Match each pattern against every complete word.

  • A lowercase letter matches that same letter.
  • Each maximal consecutive run of decimal digits is one nonnegative integer token. It consumes exactly that many arbitrary characters.
  • A token 0 consumes no characters. Multi-digit tokens have no leading zero. All patterns follow this valid grammar.

For example, b2e matches base, while 10 consumes ten characters. There is no star wildcard and no choice of skip length.

Return an integer array for each query, containing the zero-based indices of its matching words in increasing order. Preserve repeated word occurrences and repeated queries. An empty pattern matches only an empty word. If no word matches a query, its result is an empty array. Do not modify either input.

Implement both a direct dictionary scan and a method that preprocesses the dictionary once for the query batch. The batch interface and grammar details are practice conventions completing the verbal prompt.

Function

numericWildcardMatches(words: String[], patterns: String[]) → int[][]

Examples

Example 1

words = ["base","bake","be","basse","bee"]patterns = ["b2e","b1e","4"]return = [[0,1],[4],[0,1]]

The source example b2e matches base. It also matches bake, because2consumes exactly two characters; b1e matches bee, and4matches exactly four-character words.

Example 2

words = ["","a","ab","a"]patterns = ["","0","1","a0","0a"]return = [[0],[0],[1,3],[1,3],[1,3]]

An empty pattern or zero skip matches only the empty word. Duplicate word occurrences keep their separate indices; zero skips consume no characters.

Example 3

words = ["abcdefghij","abcdefghijk","abxyzghij","abcdefghij"]patterns = ["10","ab3ghij","a9"]return = [[0,3],[2],[0,3]]

The consecutive digits10form one ten-character skip, not skips1and0. Full-word length and literal offsets determine each result.

Constraints

  • 0 <= words.length <= 200 and 0 <= patterns.length <= 50.
  • Each word has between 0 and 100 lowercase ASCII letters.
  • Each pattern has between 0 and 100 lowercase letters or decimal digits. A maximal digit run is either 0 or begins with 1 through 9.
  • A numeric token may be larger than a machine integer. Such a query cannot match if its required length exceeds 100; do not overflow while parsing it.
  • All inputs are valid. These limits and output conventions are authored practice assumptions, not original interview constraints.

More Headlands Technologies problems

See Headlands Technologies hiring insights
public int[][] numericWildcardMatches(String[] words, String[] patterns) {
    // write your code here
}
words["base","bake","be","basse","bee"]
patterns["b2e","b1e","4"]
expected[[0,1],[4],[0,1]]
Checking account…