Easy · Bidirectional Mapping
Understand → Visualise → Practice.
Return whether a space-separated sentence follows a one-to-one character-to-word pattern.
Split the sentence into words. Each pattern letter must always map to the same word, and each word to the same letter, so check both directions.
If the number of words differs from the pattern length, return false.
For each (letter, word), the letter must map to the word and the word back to the letter.
Any conflict means false.
O(n) time and O(n) space for n characters in the sentence.
Mapping only letter → word accepts "ab" with "dog dog".
Read the full guide first: Hash Maps and Hash Sets.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.