Skip to content

Commit c48140d

Browse files
add 890
1 parent 50eebc4 commit c48140d

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Your ideas/fixes/algorithms are more than welcome!
5757
|900|[RLE Iterator](https://leetcode.com/problems/rle-iterator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_900.java) | O(n) | O(1) | |Medium|
5858
|897|[Increasing Order Search Tree](https://leetcode.com/problems/increasing-order-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_897.java) | O(n) | O(n) | |Easy| DFS, recursion
5959
|896|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_896.java) | O(n) | O(1) | |Easy|
60+
|890|[Find and Replace Pattern](https://leetcode.com/problems/find-and-replace-pattern/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_890.java) | O(n) | O(1) | |Medium|
6061
|884|[Uncommon Words from Two Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_884.java) | O(n) | O(k) | |Easy|
6162
|876|[Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_876.java) | O(n) | O(1) | |Easy|
6263
|872|[Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_872.java) | O(n) | O(h) | |Easy| DFS, recursion
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Set;
9+
10+
/**
11+
* 890. Find and Replace Pattern
12+
*
13+
* You have a list of words and a pattern, and you want to know which words in words matches the pattern.
14+
* 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.
15+
* (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.)
16+
* Return a list of the words in words that match the given pattern.
17+
* You may return the answer in any order.
18+
*
19+
* Example 1:
20+
*
21+
* Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
22+
* Output: ["mee","aqq"]
23+
* Explanation: "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}.
24+
* "ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation,
25+
* since a and b map to the same letter.
26+
*
27+
* Note:
28+
* 1 <= words.length <= 50
29+
* 1 <= pattern.length = words[i].length <= 20
30+
*/
31+
public class _890 {
32+
public static class Solution1 {
33+
public List<String> findAndReplacePattern(String[] words, String pattern) {
34+
List<String> result = new ArrayList<>();
35+
for (String word : words) {
36+
Map<Character, Character> map = new HashMap<>();
37+
Set<Character> set = new HashSet<>();
38+
boolean match = true;
39+
for (int i = 0; i < pattern.length(); i++) {
40+
if (map.containsKey(pattern.charAt(i))) {
41+
if (word.charAt(i) != map.get(pattern.charAt(i))) {
42+
match = false;
43+
break;
44+
}
45+
} else {
46+
map.put(pattern.charAt(i), word.charAt(i));
47+
if (!set.add(word.charAt(i))) {
48+
match = false;
49+
}
50+
}
51+
}
52+
if (match) {
53+
result.add(word);
54+
}
55+
}
56+
return result;
57+
}
58+
}
59+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._890;
4+
import java.util.Arrays;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _890Test {
11+
private static _890.Solution1 solution1;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _890.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertEquals(Arrays.asList("mee", "aqq"),
21+
solution1.findAndReplacePattern(new String[] {"abc", "deq", "mee", "aqq", "dkd", "ccc"},
22+
"abb"));
23+
}
24+
}

0 commit comments

Comments
 (0)