---
# System prepended metadata

title: 0890. Find and Replace Pattern
tags: [Leetcode, HashMap, Medium]

---

# 0890. Find and Replace Pattern
###### tags: `Leetcode` `HashMap` `Medium`
Link: https://leetcode.com/problems/find-and-replace-pattern/
## 思路
注意比较两个array的时候用**Arrays.equal()**
getPattern函数是把每个字母按照出现的顺序编码 例如abb,就是011
所以```abb```和```mpp```进去之后是一样的编码
## Code
```java=
class Solution {
    public List<String> findAndReplacePattern(String[] words, String pattern) {
        List<String> ans = new ArrayList<>();
        int[] p = getPattern(pattern);
        for(String word: words){
            int[] p2 = getPattern(word);
            if(Arrays.equals(p, p2)){
                ans.add(word);
            }
        }
        return ans;
    }
    private int[] getPattern(String word){
        int[] pattern = new int[word.length()];
        Map<Character, Integer> map = new HashMap<>();
        for(int i=0; i<word.length(); i++){
            char c = word.charAt(i);
            map.putIfAbsent(c, map.size());
            pattern[i] = map.get(c);
        }
        return pattern;
    }
}
```