package com.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//https://leetcode-cn.com/contest/weekly-contest-98/problems/find-and-replace-pattern/
//890. 查找和替换模式
public class Lesson890 {
public static void main(String[] args) {
String[] words = {"abc","deq","mee","aqq","dkd","ccc"};
String pattern = "abb";
List<String> res = findAndReplacePattern(words, pattern);
System.out.println(res);
}
public static List<String> findAndReplacePattern(String[] words, String pattern) {
List<String> list = new ArrayList<>(8);
String ptn = getPattern(pattern);
System.out.println(ptn);
for(int i=0;i<words.length;i++) {
String word = words[i];
String pattern1 = getPattern(word);
if (ptn.equals(pattern1)) {
list.add(word);
}
}
return list;
}
private static String getPattern(String pattern) {
String ptn = "";
Map<String, Integer> map = new HashMap<>(32);
for(int i=0;i<pattern.length();i++) {
String substring = pattern.substring(i, i + 1);
Integer integer = map.get(substring);
if (integer == null) {
int size = map.size();
map.put(substring, size + 1);
}
Integer integer_2 = map.get(substring);
ptn = ptn+integer_2;
}
return ptn;
}
}