Advertisement

Word Pattern - LeetCode 290 Solution

Word Pattern - Complete Solution Guide

Word Pattern is LeetCode problem 290, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given a pattern and a string s , find if s follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s . Specifically: Each letter in pattern maps to exactly one unique word in s . Each unique word in s maps to exactly one letter in pattern . No two letters map to the same word, and no two words map to the same letter. Example 1: Input: pattern = "abba", s = "dog cat cat dog" Output: true Explanation: The bijectio

Detailed Explanation

The problem asks whether a given string `s` follows the same pattern as a given pattern string `pattern`. 'Following the pattern' means there's a one-to-one mapping between each character in `pattern` and a non-empty word in `s`. This mapping must be consistent throughout the strings; no character can map to multiple words, and no word can map to multiple characters. The words in `s` are separated by single spaces. The input `pattern` contains only lowercase English letters, and `s` contains only lowercase English letters and spaces. The output is a boolean value: `true` if `s` follows the pattern, and `false` otherwise.

Solution Approach

The solution uses two hash maps (or dictionaries) to track the mappings between characters in `pattern` and words in `s`. It iterates through the input strings simultaneously. For each character and corresponding word, it checks if the mapping already exists. If it does, it verifies the consistency of the mapping. If not, it adds the new mapping to both maps. If any inconsistency is found (a character maps to multiple words or vice-versa), the function immediately returns `false`. Otherwise, after iterating through all characters and words, it returns `true`. The C solution uses arrays instead of hash tables due to C's limitations.

Step-by-Step Algorithm

  1. Step 1: Split the input string `s` into an array of words using `s.split()`. Check if the lengths of the `pattern` and the word array are different. If they are, return `false`.
  2. Step 2: Initialize two hash maps (or dictionaries), `p_map` and `w_map`, to store the character-to-word and word-to-character mappings, respectively.
  3. Step 3: Iterate through both the `pattern` and the `words` array simultaneously using a loop. For each character `p` and word `w` at the same index:
  4. Step 4: Check if `p` is already in `p_map`. If it is not, check if `w` is in `w_map`. If `w` is in `w_map`, there is a conflict (a word mapping to multiple characters), so return `false`. Otherwise, add the mapping (`p` to `w` and `w` to `p`) to both maps.
  5. Step 5: If `p` is in `p_map`, check if the mapped word is equal to `w`. If not, there is a conflict (a character mapping to multiple words), so return `false`.
  6. Step 6: After the loop completes, return `true` because all mappings are consistent.

Key Insights

  • Insight 1: Using two hash maps (or dictionaries) is crucial for efficiently checking the bijective mapping. One map tracks the character-to-word mapping, and the other tracks the word-to-character mapping. This ensures that both conditions (each character maps to exactly one word, and each word maps to exactly one character) are satisfied.
  • Insight 2: The solution needs to handle the case where the number of words in `s` doesn't match the number of characters in `pattern`. This is a quick pre-check to avoid unnecessary processing.
  • Insight 3: Empty words are not allowed, so the `s.split()` method conveniently handles this case, as it only generates non-empty words.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Hash Table, String.

Companies

Asked at: Dropbox, Google, Zoho.