Longest Match Tokenizer

Asked at GooglePhone screen

Reported by the problem's creator, not verified

You are given a string text and a dictionary that maps words to integer ids.

Examples

  1. Example 1

    Input: {"text":"theater","dictionary":{"a":3,"the":1,"theater":2}}

    Output: [2]

    Explanation: Both the and theater start at position 0, and theater is longer, so the result is its id alone.

  2. Example 2

    Input: {"text":"the theater is a","dictionary":{"a":3,"the":1,"theater":2}}

    Output: [1," ",2," ","i","s"," ",3]

    Explanation: the gives 1, the space matches nothing and stays as " ", theater gives 2, then the space, i and s and another space stay as characters, and a gives 3.

  3. Example 3

    Input: {"text":"abcx","dictionary":{"c":3,"ab":1,"abcd":2}}

    Output: [1,3,"x"]

    Explanation: abcd would be the longest word at position 0, but the text there is abcx, so the longest word that matches is ab. Then c matches, and x matches nothing.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.