Find and Replace Pattern LeetCode solution in Java

1
class Solution {
2
public List<String> findAndReplacePattern(String[] words, String pattern) {
3
List<String> result = new ArrayList<>();
4
for (String word : words) {
5
Map<Character, Character> map = new HashMap<>();
6
Set<Character> set = new HashSet<>();
7
int i = 0;
8
for (; i < word.length(); i++) {
9
char ch = pattern.charAt(i);
10
if (map.get(ch) == null) {
11
if (set.contains(word.charAt(i))) break;
12
map.put(ch, word.charAt(i));
13
set.add(word.charAt(i));
14
} else {
15
char mc = map.get(ch);
16
if (mc != word.charAt(i)) break;
17
}
18
}
19
if (i == pattern.length()) result.add(word);
20
}
21
return result;
22
}
23
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0