Advertisement

Find and Replace Pattern - LeetCode 890 Solution

Find and Replace Pattern - Complete Solution Guide

Find and Replace Pattern is LeetCode problem 890, a Medium 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 list of strings words and a string pattern , return a list of words[i] that match pattern . You may return the answer in any order . A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x) , we get the desired word. Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter. Example 1: Input: words = ["abc","deq","m

Detailed Explanation

The problem asks us to find all words in a given list that match a specified pattern. A word matches the pattern if there exists a one-to-one mapping (bijection or permutation) between the letters of the pattern and the letters of the word. This means each letter in the pattern must map to a unique letter in the word, and vice-versa. The length of the pattern and each word is the same and consist of lowercase English letters. The goal is to return a list of words that satisfy this matching condition.

Solution Approach

The provided code implements a normalization technique to solve this problem. It normalizes both the input pattern and each word in the input list. It then compares the normalized representations. If the normalized representations match, the word is considered a match for the pattern and added to the result list. The normalization process involves creating a mapping from characters to indices based on their first appearance in the string. This allows for a direct comparison of string structures rather than character values.

Step-by-Step Algorithm

  1. Step 1: Define a function `normalize(s)` that takes a string `s` as input.
  2. Step 2: Inside `normalize(s)`, create an empty dictionary/map called `mapping` to store the character-to-index mapping.
  3. Step 3: Iterate through the string `s` character by character.
  4. Step 4: For each character `c`, check if it exists as a key in the `mapping`.
  5. Step 5: If `c` is not in `mapping`, add it to `mapping` with a value equal to the current size of `mapping` (which is effectively the next available index).
  6. Step 6: Append the index (value from `mapping`) corresponding to the character `c` to the normalized list.
  7. Step 7: Return the normalized list.
  8. Step 8: Normalize the input `pattern` using the `normalize` function and store the result as `pattern_key`.
  9. Step 9: Initialize an empty list `result` to store the matching words.
  10. Step 10: Iterate through the `words` list.
  11. Step 11: For each `word` in `words`, normalize it using the `normalize` function.
  12. Step 12: Compare the normalized representation of the `word` with `pattern_key`.
  13. Step 13: If they are equal, append the `word` to the `result` list.
  14. Step 14: Return the `result` list.

Key Insights

  • Insight 1: The core idea is to normalize both the pattern and the words. Normalization transforms each string into a sequence of integers representing the order of appearance of each unique character. For instance, 'abb' becomes [0, 1, 1] because 'a' is the first unique character (0), and 'b' is the second (1) which repeats.
  • Insight 2: By normalizing, we effectively remove the specific characters themselves and focus solely on the structure of the string, represented by the order of unique character appearances.
  • Insight 3: The one-to-one mapping requirement is crucial. If the mapping isn't a bijection (e.g., two different letters in the pattern map to the same letter in the word), the word doesn't match.

Complexity Analysis

Time Complexity: O(n*m)

Space Complexity: O(m)

Topics

This problem involves: Array, Hash Table, String.

Companies

Asked at: Zomato.