1ms HashMap/HashSet Java Code. Easy to Understand.
class Solution {
    public List<String> findAndReplacePattern(String[] words, String p) {
        
        List<String> list = new ArrayList<>();
        
        int j=0,len = p.length();
        char ch,ch2;//variables just to store the charachers of words and pattern(easy to code lol)
        
        for(int i=0;i<words.length;i++){
            HashSet<Character> hs = new HashSet<>();                         //Used to check if the character is repeated or not
            HashMap<Character,Character> hm = new HashMap<>();    //Used for checking pattern matching
            for(j=0;j<len;j++){
                ch = words[i].charAt(j);//character assignment 
                ch2 = p.charAt(j);//character assignment 
				
                if(!hm.containsKey(ch2)){          // Pattern Matching Logic begins here
                    if(!hs.contains(ch)){               //Repetition logic begins here
                    hm.put(ch2,ch);
                    hs.add(ch);
                    }                                            //Repetition Logic ends here
                    else
                        break;
                }
                else{
                    if(ch!=hm.get(ch2)) 
                        break;
                }                                              //Pattern matching logic ends here
            }
            
            if(j==len)                                   //If the mattern is matching, word will be added to the list.
                list.add(words[i]);
        }
        
        return list;
        
    }
}```
Comments (0)