Decode Words with Anagram Keys
Updated
Problem statement
You are given a list of lowercase knownWords and an encodedMessage. For this exercise, assume each encoded token is a rearrangement of a known word’s letters that preserves that word’s first letter.
Decode each token independently. A word is a candidate exactly when it has the same first letter and the same number of every letter as the token. If several known words are candidates, choose the one that comes first alphabetically. Listing the same known word more than once does not change the choices.
Every nonempty message token is guaranteed to have at least one candidate. The message is empty or contains nonempty tokens separated by single spaces, with no leading or trailing space. Return the decoded words joined by single spaces, or the empty string for an empty message.
Function
decodeMessage(knownWords: String[], encodedMessage: String) → StringExamples
Example 1
knownWords = ["cat","dog"]encodedMessage = "cta dgo"return = "cat dog"The tokens preserve c and d and decode to cat dog.
Example 2
knownWords = ["east","eats","tea"]encodedMessage = "etsa tae"return = "east tea"Both east and eats match the first token; choose east. The second token decodes to tea.
Example 3
knownWords = []encodedMessage = ""return = ""An empty message has no tokens to decode.
Constraints
0 <= knownWords.length <= 2000.- Each known word and encoded token contains
1..40lowercase English letters. - The total number of letters in the dictionary is at most
8 × 10^4. - The message contains at most
2000tokens and at most10^5characters including spaces.
More Cresta interview questions
- Find a File in a Directory SnapshotOA · Seen Oct 2026
- Leaves Remaining After WindOA · Seen Oct 2026
- Maximum Suffix–Prefix OverlapOA · Seen Oct 2026
- Particle Moves to TargetOA · Seen Jul 2026
- Rightward Bacteria ConsumptionOA · Seen Jul 2026
- Top Three Companies by ProfitOA · Seen Jul 2026
- Peak Chat Load Across AgentsPHONE SCREEN · Seen Nov 2026
- File Storage ServicePHONE SCREEN · Seen Nov 2026
- Dynamic Least-Loaded Job DispatcherPHONE SCREEN · Seen Oct 2026
- Feature Flag Evaluation EngineONSITE INTERVIEW · Seen Jul 2026
Similar questions at other companies
- Longest String ChainVisa · OAVisa interview questions
- Frequently Bought TogetherAmazon · OA
- Maximize Palindromic WordsSquarepoint Capital · OASquarepoint Capital interview questions
- Aggregate Paginated Project Run RecordsReplit · OA
- Parse and Filter Chargeback DisputesStripe · OAStripe interview questions
- Count User Logins 🌿 (Singapore)PayPal · OAPayPal interview questions
- Largest Lexicographical Substring 🌿Google · OAGoogle interview questions
- Count Distinct Message MentionsZipRecruiter · OAZipRecruiter interview questions
- Rank Common Translation MistakesDuolingo · OADuolingo interview questions
- Aggregate Investment Ownership from CSVCarta · OA · PHONE SCREEN