All problems
WordPiece Longest Piece Length
Problem
Run a greedy WordPiece scan over word with the given vocab and report the longest piece that matches. At each position take the longest prefix found in vocab, otherwise advance a single character.
Return the maximum matched piece length, or 0 when word is empty or nothing matches.
Hint
Sweep end from the full remaining string down to i and take the first hit.
Test cases
case 1
in: ["playing",["play","ing","p","l","a","y","i","n","g"]]
expected: 4
case 2
in: ["unwanted",["un","want","ed"]]
expected: 4
case 3
in: ["abc",["a"]]
expected: 1
case 4
in: ["",["a"]]
expected: 0
case 5
in: ["zz",[]]
expected: 0
Starter codePython
def wordpiece_longest_piece(word, vocab):
# Your code here
pass5 test cases · run your code against them in the editor