1. 程式人生 > >890. Find and Replace Pattern

890. Find and Replace Pattern

You have a list of words and a pattern, and you want to know which words in words matches the pattern.

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.)

Return a list of the words in words that match the given pattern. 

You may return the answer in any order.

Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
Output: ["mee","aqq"]
Explanation: 
"mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}. "ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation, since a and b map to the same letter.
  • 1 <= words.length <= 50
  • 1 <= pattern.length = words[i].length <= 20

解:

    題目挺長的,意思就是說, 對於給定的pattern,在words的所有string中,找出符合這個模式的所有string,符合這個模式的意思就是如果pattern是 “abb”,那麼 “acc”、“pee”、“dzz”,這種都是符合的。其他情況就不符合,我總結了一下,從pattern往word對映為主,不符合模式的大概可以分為兩種:

    1、“ccc”,這樣pattern中 a 對映到 c,b 也對映到 c,不可以pattern中兩個不同字元對映到word中同一個字元;

    2、“deq”,這樣pattern中 a 對映到 d,b 對映到 e,第二個 b 有對映到 q,不可以pattern中一個字元對映到word中多個字元;

    所以pattern和word同時從左往右遍歷,對應位置上:

    1、如果 pattern[i] 沒有進行過對映

             如果 word[i] 已經被對映過,那麼說明沒辦法雙射了,跳過這個string;

             如果 word[i] 沒有被對映過(防止上述第一種情況發生),那麼就讓 pattern[i] 對映到 word[i];

    2、如果對映過,但是已經設定的對映 pattern[i] 不對映到 word[i] 那麼這個string一定不對,直接跳過到下一個string。

    按照上述分析,程式碼很容易實現。

vector<string> findAndReplacePattern(vector<string>& words, string pattern)
{    
    int len = pattern.length();
    vector<string> res;
    
    for(auto word : words)
    {
        vector<char> bijection(26, '*');    // pattern 到 word 中字元的對映關係,不能是反過來的
        int i = 0;
        for(; i < len; i++)
        {
            if(bijection[pattern[i] - 'a'] == '*')     // 沒有對映過
            {
                if(find(bijection.begin(), bijection.end(), word[i]) != bijection.end())    // 為了防止pattern中兩個對映到一個字元
                    break;
                else
                    bijection[pattern[i] - 'a'] = word[i];
            }
            else if(bijection[pattern[i] - 'a'] != word[i]) // 對映過了,但是對映的不是這個,這個單詞肯定就不行
                break;
        }
        if(i == len)
            res.push_back(word);
    }
    return res;
}

     思想一樣,稍微簡便一點的寫法如下:

vector<string> findAndReplacePattern(vector<string>& words, string pattern)
{    
    int len = pattern.length();
    vector<string> res;
    
    for(auto word : words)
    {
        vector<char> bijection(26, '*');    // pattern 到 word 中字元的對映關係,不能是反過來的
        int i = 0;
        for(; i < len; i++)
        {
            if(bijection[pattern[i] - 'a'] == '*' && find(bijection.begin(), bijection.end(), word[i]) == bijection.end())   // 沒有對映過
                bijection[pattern[i] - 'a'] = word[i];
            else if(bijection[pattern[i] - 'a'] == word[i])
                continue;
            else // 對映過了,但是對映的不是這個,這個單詞肯定就不行
                break;
        }
        if(i == len)
            res.push_back(word);
    }
    return res;
}