|
| 1 | +/** |
| 2 | + * You have a list of words and a pattern, and you want to know which words in |
| 3 | + * words matches the pattern. |
| 4 | + * |
| 5 | + * A word matches the pattern if there exists a permutation of letters p so |
| 6 | + * that after replacing every letter x in the pattern with p(x), we get the |
| 7 | + * desired word. |
| 8 | + * |
| 9 | + * (Recall that a permutation of letters is a bijection from letters to |
| 10 | + * letters: every letter maps to another letter, and no two letters map to |
| 11 | + * the same letter.) |
| 12 | + * |
| 13 | + * Return a list of the words in words that match the given pattern. |
| 14 | + * |
| 15 | + * You may return the answer in any order. |
| 16 | + * |
| 17 | + * Example 1: |
| 18 | + * Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb" |
| 19 | + * Output: ["mee","aqq"] |
| 20 | + * Explanation: "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}. |
| 21 | + * "ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation, |
| 22 | + * since a and b map to the same letter. |
| 23 | + * |
| 24 | + * Note: |
| 25 | + * 1 <= words.length <= 50 |
| 26 | + * 1 <= pattern.length = words[i].length <= 20 |
| 27 | + */ |
| 28 | + |
| 29 | +publicclassFindAndReplacePattern890 { |
| 30 | +publicList<String>findAndReplacePattern(String[]words,Stringpattern) { |
| 31 | +List<String>res =newArrayList<>(); |
| 32 | +char[]pat =pattern.toCharArray(); |
| 33 | +intN =pattern.length(); |
| 34 | +for (Stringword:words) { |
| 35 | +if (isPermutation(word.toCharArray(),pat,N)) { |
| 36 | +res.add(word); |
| 37 | + } |
| 38 | + } |
| 39 | +returnres; |
| 40 | + } |
| 41 | + |
| 42 | +publicbooleanisPermutation(char[]word,char[]pattern,intN) { |
| 43 | +Map<Character,Character>map =newHashMap<>(); |
| 44 | +for (inti=0;i<N;i++) { |
| 45 | +if (map.containsKey(word[i])) { |
| 46 | +if (map.get(word[i]) !=pattern[i])returnfalse; |
| 47 | + }else { |
| 48 | +if (map.values().contains(pattern[i]))returnfalse; |
| 49 | +map.put(word[i],pattern[i]); |
| 50 | + } |
| 51 | + } |
| 52 | +returntrue; |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | +publicList<String>findAndReplacePattern2(String[]words,Stringpattern) { |
| 57 | +List<String>res =newArrayList<>(); |
| 58 | +intcode =encode(pattern); |
| 59 | +for (Stringw:words) { |
| 60 | +if (code ==encode(w)) { |
| 61 | +res.add(w); |
| 62 | + } |
| 63 | + } |
| 64 | +returnres; |
| 65 | + } |
| 66 | + |
| 67 | +publicintencode(Strings) { |
| 68 | +intres =0; |
| 69 | +char[]chars =s.toCharArray(); |
| 70 | +charfirst =chars[0]; |
| 71 | +Map<Integer,Integer>map =newHashMap<>(); |
| 72 | +map.put(0,0); |
| 73 | +inti =1; |
| 74 | +for (intj=0;j<chars.length;j++) { |
| 75 | +intoffset =chars[j] -first; |
| 76 | +if (map.containsKey(offset)) { |
| 77 | +res +=map.get(offset) *j; |
| 78 | + }else { |
| 79 | +map.put(offset,i); |
| 80 | +res +=i *j; |
| 81 | +i++; |
| 82 | + } |
| 83 | + } |
| 84 | +returnres; |
| 85 | + } |
| 86 | + |
| 87 | +} |