-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
29 lines (25 loc) · 1.07 KB
/
Solution.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
class Solution {
public List<String> findAndReplacePattern(String[] words, String pattern) {
int length = pattern.length();
List<String> res = new ArrayList<String>();
for (String word : words) {
Map<Character, Character> charMap = new HashMap<Character, Character>(),
charReverseMap = new HashMap<Character, Character>();
boolean notMatch = false;
for (int i = 0; i < length; i++) {
char char1 = word.charAt(i),
char2 = pattern.charAt(i);
if (charMap.get(char1) == null && charReverseMap.get(char2) == null) {
charMap.put(char1, char2);
charReverseMap.put(char2, char1);
} else if (charMap.get(char1) == null || !charMap.get(char1).equals(char2)) {
notMatch = true;
break;
}
}
if (!notMatch)
res.add(word);
}
return res;
}
}