easyHashingPattern: Hash Map / Bijection
Symbol Pattern Verification Solution
Problem Statement
Given a symbol pattern and a sentence, verify if the sentence follows the exact structure of the pattern, where each symbol maps to exactly one word and vice versa.
Examples
Example 1:
Input:{"pattern":"abba","sentence":"dog cat cat dog"}
Output:true
Explanation: Each symbol maps to exactly one word and vice versa
Example 2:
Input:{"pattern":"abba","sentence":"dog cat cat fish"}
Output:false
Explanation: Symbol 'a' maps to different words 'dog' and 'fish'
Constraints
- 1 <= pattern.length <= 300
- s contains lowercase English letters separated by a single space.
Time: O(N) Space: O(N)
Split string by space. Use two HashMaps: charToWord and wordToChar. If mappings conflict, return false. Time O(N), Space O(N).
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
No solution code is currently loaded.
Complete this code in the workspace editor.
