forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_890.java
38 lines (36 loc) · 1.25 KB
/
_890.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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;
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;
}
}
}