LeetCode Find and Replace Pattern 查找和替换模式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sun_wangdong/article/details/83118972

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.

Example 1:

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.

Note:

  • 1 <= words.length <= 50
  • 1 <= pattern.length = words[i].length <= 20

你有一个单词列表 words 和一个模式  pattern,你想知道 words 中的哪些单词与模式匹配。

如果存在字母的排列 p ,使得将模式中的每个字母 x 替换为 p(x) 之后,我们就得到了所需的单词,那么单词与模式是匹配的。

(回想一下,字母的排列是从字母到字母的双射:每个字母映射到另一个字母,没有两个字母映射到同一个字母。)

返回 words 中与给定模式匹配的单词列表。

你可以按任何顺序返回答案。

示例:

输入:words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
输出:["mee","aqq"]
解释:
"mee" 与模式匹配,因为存在排列 {a -> m, b -> e, ...}。
"ccc" 与模式不匹配,因为 {a -> c, b -> c, ...} 不是排列。
因为 a 和 b 映射到同一个字母。

提示:

  • 1 <= words.length <= 50
  • 1 <= pattern.length = words[i].length <= 20

题解,给定一个字符串数组和一个匹配的pattern字符串,判断字符串数组中的每一个字符串是否符合该pattern的模式。

例如有一个字符串数组为:["abc","deq","mee","aqq","dkd","ccc"],pattern为:"abb",那么需要在字符串数组中去一一遍历寻找,第一个"abc",它的构成模式不符合"abb"的模式,故略去;依次往后走,直到"mee",这个字符满足"abb"的模式,也就是第二和第三个字符相同,那么将它压入到list中;继续往后走,直到"aqq",压入list中。

那么看到这道题,我想到的解题思路是构造一个hashmap,遍历每一个字符串,将该字符串中的每一个字符都和pattern中的每一个字符进行一一对应,例如,第一个"abc",则遍历之后,a对应pattern中的a;第二个"b"遍历后对应pattern中的第而个"b";但是第三个"c"和pattern中的"b"不对应,所以该字符不能压入list中。所以hashmap以pattern中的每个字符为key,以字符串中的每个字符为value,进行遍历。但是这里需要特别注意的地方是凡是pattern中不一样的字符,所对应的字符串中的每一个字符必须不一样,哪怕对应到字符串中的字符没有在hashmap中出现过,这需要单独判断!!!

这个小错误很容易忽视,从而引起大的问题。

public static List<String> findAndReplacePattern(String[] words, String pattern)
    {
        List<String> list = new ArrayList<>();
        for(int i = 0; i < words.length; i++)
        {
            if(words[i].length() != pattern.length())
                continue;
            Map<Character,Character> hashMap = new HashMap<>();
            boolean isTest = true;
            for(int j = 0; j < pattern.length(); j++)
            {
                if(!hashMap.containsKey(pattern.charAt(j)))
                {
                    if(!hashMap.containsValue(words[i].charAt(j)))    //这种情况很容易遗漏,当某个字符从来没有在hashmap的key中出现过时,还要判断hashmap中的value是否出现将要插入的words[i].charat(j)的字符,如果有,那说明肯定和之前某个字符重复了,但这和pattern重复了
                        hashMap.put(pattern.charAt(j),words[i].charAt(j));
                    else
                    {
                        isTest = false;
                        break;
                    }
                }
                else
                {
                    if(hashMap.get(pattern.charAt(j)) != words[i].charAt(j))
                    {
                        isTest = false;
                        break;
                    }
                }
            }
            if(isTest == true)
                list.add(words[i]);
        }
        return list;
    }

猜你喜欢

转载自blog.csdn.net/sun_wangdong/article/details/83118972