Sign Shop Letter Blocks
A sign shop builds lettering from pre-cut wooden blocks: single letters, plus blocks for common groups like th, ing and bake, since one block mounts faster than four. The workbench rule is that, reading left to right, you always take the longest block that fits the next letters exactly, or a single letter if none does.
Some language-model tokenizers use the same longest-match-first rule to cut text into pieces from a fixed vocabulary. That is why a common word is often one token while a rare word breaks into several.
Your task
Write tokenize(text, pieces). Start at the first letter. At each position, find the longest string in pieces that appears in text starting exactly there. Add it to the result and move past it. If no piece matches, add the single letter at that position and move one step.
Return the list of tokens in order. Joined together they must spell text. An empty text gives an empty list.
tokenize(text: string, pieces: string[]) → string[]
Examples
text = "bakery" pieces = ["ba", "bake", "ry", "er", "y"]
["bake", "ry"]
At position 0 both "ba" and "bake" fit, and the longer one, "bake", is taken. At position 4 the piece "ry" fits, while "er" does not start there. The result is ["bake", "ry"].
text = "thinking" pieces = ["th", "in", "ink", "ing", "king"]
["th", "ink", "ing"]
At position 0 the piece "th" fits. At position 2 both "in" and "ink" fit, and the longer one, "ink", is taken. At position 5 the piece "ing" fits. The result is ["th", "ink", "ing"]. The greedy rule does not look ahead, so it never considers "in" followed by "king", even though that split would also cover the text.
text = "zoo" pieces = ["oo", "bake"]
["z", "oo"]
No piece starts with "z", so it becomes a single-letter token. Then "oo" fits, so the result is ["z", "oo"].
Constraints
- 0 ≤ text.length ≤ 10,000
- 0 ≤ pieces.length ≤ 1,000
- 1 ≤ pieces[i].length ≤ 20
- text and pieces use lowercase letters a to z only. pieces may contain repeats.
Start with brute force
At every position, check every piece and keep the longest that starts there. With 10,000 letters and 1,000 pieces of up to 20 letters, that is up to 200 million character comparisons.
Only about 20 lengths are possible at any position, and a set says whether a chunk is a piece in roughly one step, so you can check those few lengths instead of checking every piece.
Hints
0 of 4 shown- Hint 1
In the first example, "ba" and "bake" both fit at position 0. If you took the first piece in the given order that fits, which would you pick?
- Hint 2
The rule asks for the longest match, so the order of
piecesmust not matter. You could check every piece at every position and keep the longest. How many checks is that for a long text and a big vocabulary? - Hint 3
Reverse the question. Instead of asking every piece whether it fits at this position, look at the text and ask whether the next 4-letter chunk is a piece, then the next 3-letter chunk, then the next 2-letter chunk. A set answers each of these in one step.
- Hint 4
Put the pieces in a set and note the longest piece length
L. At positioni, try lengths frommin(L, remaining)down to 2 and take the first substring that is in the set. If none is, take one letter. Then advanceiby the length taken.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
The greedy rule never undoes a choice, so the only question is how to find the longest fitting piece quickly.
Trace "bakery" with pieces ["ba", "bake", "ry", "er", "y"]. The longest piece has 4 letters, so L = 4.
- At i = 0, try "bake" (4 letters). It is in the set, so the token is "bake" and i becomes 4.
- At i = 4 only 2 letters are left, so try "ry". It is in the set, so the token is "ry" and i becomes 6.
- At i = 6 the text is finished, and the result is ["bake", "ry"].
Greedy matching is fast and predictable, but it does not always give the fewest tokens. With pieces ab, abc and cd, the text "abcd" becomes "abc" and "d" instead of "ab" and "cd". Real tokenizers accept that trade-off. A trie would avoid re-reading letters, but with short pieces the set is simpler.
function tokenize(text, pieces) {
const vocab = new Set(pieces);
let longest = 0;
for (const p of pieces) longest = Math.max(longest, p.length);
const tokens = [];
let i = 0;
while (i < text.length) {
let len = Math.min(longest, text.length - i);
// Try the longest possible piece first, then shorter ones.
while (len > 1 && !vocab.has(text.slice(i, i + len))) len--;
len = Math.max(len, 1); // a single letter is always allowed
tokens.push(text.slice(i, i + len));
i += len;
}
return tokens;
}def tokenize(text, pieces):
vocab = set(pieces)
longest = max((len(p) for p in pieces), default=1)
tokens = []
i = 0
while i < len(text):
length = min(longest, len(text) - i)
# Try the longest possible piece first, then shorter ones.
while length > 1 and text[i:i + length] not in vocab:
length -= 1
length = max(length, 1) # a single letter is always allowed
tokens.append(text[i:i + length])
i += length
return tokensRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.