package com.fishercoder.solutions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 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.
 *
 *  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
 */
public class _890 {
  public static class Solution1 {
    public List<String> findAndReplacePattern(String[] words, String pattern) {
      List<String> result = new ArrayList<>();
      for (String word : words) {
        Map<Character, Character> map = new HashMap<>();
        Set<Character> set = new HashSet<>();
        boolean match = true;
        for (int i = 0; i < pattern.length(); i++) {
          if (map.containsKey(pattern.charAt(i))) {
            if (word.charAt(i) != map.get(pattern.charAt(i))) {
              match = false;
              break;
            }
          } else {
            map.put(pattern.charAt(i), word.charAt(i));
            if (!set.add(word.charAt(i))) {
              match = false;
            }
          }
        }
        if (match) {
          result.add(word);
        }
      }
      return result;
    }
  }
}

@@ -181,1025 +181,8 @@
 t;%0A%0A
-/**%0A * 890. Find and Replace Pattern%0A *%0A * You have a list of words and a pattern, and you want to know which words in words matches the pattern.%0A * 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.%0A * (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.)%0A * Return a list of the words in words that match the given pattern.%0A * You may return the answer in any order.%0A *%0A *  Example 1:%0A *%0A * Input: words = %5B%22abc%22,%22deq%22,%22mee%22,%22aqq%22,%22dkd%22,%22ccc%22%5D, pattern = %22abb%22%0A * Output: %5B%22mee%22,%22aqq%22%5D%0A * Explanation: %22mee%22 matches the pattern because there is a permutation %7Ba -%3E m, b -%3E e, ...%7D.%0A * %22ccc%22 does not match the pattern because %7Ba -%3E c, b -%3E c, ...%7D is not a permutation,%0A * since a and b map to the same letter.%0A *%0A * Note:%0A * 1 %3C= words.length %3C= 50%0A * 1 %3C= pattern.length = words%5Bi%5D.length %3C= 20%0A */%0A
 publ
@@ -197,16 +197,18 @@
  _890 %7B%0A
+  
   public
@@ -233,16 +233,20 @@
 tion1 %7B%0A
+    
     publ
@@ -323,16 +323,22 @@
 %7B%0A      
+    
+  
 List%3CStr
@@ -366,24 +366,30 @@
 ayList%3C%3E();%0A
+      
       for (S
@@ -418,16 +418,24 @@
         
+  
+      
 Map%3CChar
@@ -471,24 +471,32 @@
 ashMap%3C%3E();%0A
+        
         Set%3C
@@ -537,16 +537,24 @@
         
+     
+   
 boolean 
@@ -567,16 +567,24 @@
 = true;%0A
+        
         
@@ -634,24 +634,34 @@
 %7B%0A          
+         
+ 
 if (map.cont
@@ -682,32 +682,44 @@
 n.charAt(i))) %7B%0A
+            
             if (
@@ -772,32 +772,46 @@
 %7B%0A              
+      
+        
 match = false;%0A 
@@ -823,16 +823,30 @@
         
+            
+  
 break;%0A 
@@ -848,34 +848,51 @@
 ak;%0A            
+        
-%7D%0A
+    %7D%0A     
           %7D else
@@ -885,16 +885,21 @@
         
+     
 %7D else %7B
@@ -899,16 +899,28 @@
  else %7B%0A
+            
         
@@ -975,24 +975,36 @@
             
+   
+         
 if (!set.add
@@ -1029,32 +1029,46 @@
 %7B%0A              
+          
+    
 match = false;%0A 
@@ -1082,40 +1082,75 @@
     
-%7D%0A          %7D%0A        %7D%0A
+            %7D%0A                    %7D%0A                %7D%0A     
         
 if (
@@ -1141,24 +1141,27 @@
             
+   
 if (match) %7B
@@ -1171,16 +1171,26 @@
         
+       
+   
 result.a
@@ -1207,24 +1207,32 @@
         
+       
+ 
 %7D%0A      
 %7D%0A      
@@ -1223,24 +1223,36 @@
 %7D%0A      
+    
-%7D%0A
+  %7D%0A   
       
+   
 return r
@@ -1262,16 +1262,22 @@
 lt;%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 890