LeetCode - 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。

    按照上述分析,代码很容易实现。

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'] == '*')     // 没有映射过
            {
                // 为了防止pattern中两个映射到一个字符
                if(find(bijection.begin(), bijection.end(), word[i]) != bijection.end())
                    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> biject(26, '*');    // pattern 到 word 中字符的映射关系,不能是反过来的
        int i = 0;
        for(; i < len; i++)
        {
            // 没有映射,也没有被映射
            if(biject[pattern[i] - 'a'] == '*' && find(biject.begin(), biject.end(), word[i]) == biject.end())
                biject[pattern[i] - 'a'] = word[i];
            else if(biject[pattern[i] - 'a'] == word[i])
                continue;
            else // 映射过了,但是映射的不是这个,这个单词肯定就不行
                break;
        }
        if(i == len)
            res.push_back(word);
    }
    return res;
}

猜你喜欢

转载自blog.csdn.net/Bob__yuan/article/details/82631278