You are given a text string and a dictionary of token-to-id mappings. Starting from the beginning of text, repeatedly choose the longest dictionary token that matches the current position and output its id. If no dictionary token matches, output the current character itself and advance by one character.
Return the sequence of emitted ids and literal characters.
Examples
01 · Example 1
text = "applepie" dictionary = [["app","B"],["apple","A"],["pie","P"]] return = ["A","P"]
apple is preferred over app because it is the longest match at index 0.
02 · Example 2
text = "xabcd" dictionary = [["ab","1"],["abc","2"],["bc","3"]] return = ["x","2","d"]
The first character has no match, then abc is the longest token starting at index 1.
Constraints
If multiple dictionary entries have the same token, use the first mapping provided. The tokenization is greedy and scans left to right.
More Google problems
- Consolidate On-Call RotationsOA · Seen Jun 2026
- Detonate Bombs with Chain ReactionsONSITE INTERVIEW · Seen May 2026
- Evaluate a Nested Math ExpressionONSITE INTERVIEW · Seen May 2026
- Tic-Tac-Toe Game StatusPHONE SCREEN · Seen May 2026
- Minimum Cars for Rental RequestsONSITE INTERVIEW · Seen Apr 2026
- Shortest Path with Mandatory WaypointONSITE INTERVIEW · Seen Apr 2026
- Count Divisible Coin SelectionsOA · Seen Dec 2025
- Minimum Town Sum DifferenceOA · Seen Dec 2025
public String[] encodeWithDictionary(String text, String[][] dictionary) {
// write your code here
}
text"applepie"
dictionary[["app","B"],["apple","A"],["pie","P"]]
expected["A", "P"]
sign in to submit