forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_890.java
70 lines (66 loc) · 2.51 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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;
}
}
public static class Solution2 {
public List<String> findAndReplacePattern(String[] words, String pattern) {
List<String> result = new ArrayList<>();
for (String word : words) {
if (matches(word, pattern)) {
result.add(word);
}
}
return result;
}
private boolean matches(String word, String pattern) {
Map<Character, Character> map1 = new HashMap<>();//word -> p
Map<Character, Character> map2 = new HashMap<>();//p -> word
for (int i = 0; i < pattern.length(); i++) {
if (!map1.containsKey(word.charAt(i))) {
map1.put(word.charAt(i), pattern.charAt(i));
}
if (map1.containsKey(word.charAt(i)) && map1.get(word.charAt(i)) != pattern.charAt(i)) {
return false;
}
if (!map2.containsKey(pattern.charAt(i))) {
map2.put(pattern.charAt(i), word.charAt(i));
}
if (map2.containsKey(pattern.charAt(i)) && map2.get(pattern.charAt(i)) != word.charAt(i)) {
return false;
}
}
return true;
}
}
}