BackeasyHashing HCL Zomato

Word Pattern Matcher Solution

Problem Statement

A translator maps symbol patterns to words. Given a symbol pattern and a sentence, verify if the sentence follows the exact structure of the pattern (each symbol maps to exactly one word and vice versa).

Example 1
Input
pattern = 'xyyx', sentence = 'sun moon moon sun'
Output
false

Explanation: Step 1: Split the pattern into unique characters: ['x', 'y']. Step 2: Split the sentence into words: ['sun', 'moon', 'moon', 'sun']. Step 3: Compare the length of unique characters in the pattern and sentence. Since 'x' and 'y' are unique characters in the pattern, but 'sun' and 'moon' are not unique words in the sentence, the lengths do not match. Step 4: Return false because the sentence does not follow the exact structure of the pattern.

Example 2
Input
pattern = 'abba', sentence = 'dog cat cat dog'
Output
false

Explanation: Step 1: Split the pattern into unique characters: ['a', 'b']. Step 2: Split the sentence into words: ['dog', 'cat', 'cat', 'dog']. Step 3: Compare the length of unique characters in the pattern and sentence. Since 'a' and 'b' are unique characters in the pattern, but 'dog' and 'cat' are not unique words in the sentence, the lengths do not match. Step 4: Return false because the sentence does not follow the exact structure of the pattern.

Constraints

  • 1 <= pattern.length <= 300
  • s contains lowercase English letters separated by a single space.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free