leetcode-150

Word Pattern

The Word Pattern problem is a common string manipulation challenge often asked in coding interviews. It tests your understanding of pattern matching and string mapping.

In this article, we will break down the problem, explain the approach, walk through the JavaScript solution, and provide some notes for better clarity.


🔍 Problem Statement

Given a string pattern and a string s, return true if s follows the same pattern.

Here’s how the pattern matching works:

Rules:

Examples:

wordPattern("abba", "dog cat cat dog");   // true
wordPattern("abba", "dog cat cat fish");  // false
wordPattern("aaaa", "dog cat cat dog");  // false
wordPattern("abba", "dog dog dog dog");  // false

🧩 Understanding the Algorithm

To determine if s follows the same pattern as pattern, we need to establish a one-to-one mapping between the characters in the pattern and the words in the string s.

Key Observations:

  1. Length Mismatch: If the length of pattern doesn’t match the number of words in s, return false immediately.

  2. One-to-One Mapping: Each character in pattern must map to a unique word, and no two characters in pattern can map to the same word.

  3. Reverse Mapping: Similarly, each word in s must map to a unique character in pattern.


✅ JavaScript Solution using Hash Maps

The best solution to this problem is to use two hash maps (or objects) to store the mappings from pattern to words and from words to pattern. This ensures a one-to-one mapping in both directions.

JavaScript Code:

/**
 * Determines if string `s` follows the same pattern as `pattern`.
 * @param {string} pattern - The pattern string.
 * @param {string} s - The string to check.
 * @return {boolean}
 */
var wordPattern = function(pattern, s) {
    const words = s.split(' ');
    if (pattern.length !== words.length) return false;

    const mapPatternToWord = new Map();
    const mapWordToPattern = new Map();

    for (let i = 0; i < pattern.length; i++) {
        const char = pattern[i];
        const word = words[i];

        if (mapPatternToWord.has(char)) {
            if (mapPatternToWord.get(char) !== word) return false;
        } else {
            mapPatternToWord.set(char, word);
        }

        if (mapWordToPattern.has(word)) {
            if (mapWordToPattern.get(word) !== char) return false;
        } else {
            mapWordToPattern.set(word, char);
        }
    }

    return true;
};

How it works:

  1. Step 1: First, split the string s into individual words.

  2. Step 2: Check if the length of the pattern matches the number of words. If not, return false.

  3. Step 3: Use two hash maps (mapPatternToWord and mapWordToPattern) to track the mappings between characters and words.

  4. Step 4: For each character in pattern and the corresponding word in s, ensure the mappings are consistent.

    • If the character has been seen before and doesn’t map to the same word, return false.

    • Similarly, ensure the word hasn’t been mapped to a different character.

  5. Step 5: If all checks pass, return true.


🕒 Time Complexity


📌 Notes & Gotchas


⏱ Complexity Analysis Summary

Approach Time Complexity Space Complexity
Hash Map Approach O(n) O(n)

🔗 Reference

Leetcode: Word Pattern