-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatternFinder.java
53 lines (46 loc) · 1.5 KB
/
PatternFinder.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
package org.sean.string;
import java.util.*;
/***
* 890. Find and Replace Pattern
*/
public class PatternFinder {
public List<String> findAndReplacePattern(String[] words, String pattern) {
if (words == null || pattern == null)
return Collections.emptyList();
int len = pattern.length();
String patternList = filterPattern(pattern, len);
List<String> result = new LinkedList<>();
for (String wd : words) {
if (patternList.equals(filterPattern(wd, wd.length()))) {
result.add(wd);
}
}
return result;
}
private String filterPattern(String pattern, int len) {
StringBuilder patternList = new StringBuilder();
Map<Character, Integer> map = new HashMap<>();
for (int j = 0; j < len; j++) {
char key = pattern.charAt(j);
if (!map.containsKey(key)) {
map.put(key, j);
}
}
int lastCnt = 1;
char lastChar = pattern.charAt(0);
for (int i = 1; i < len; i++) {
char ch = pattern.charAt(i);
if (ch != lastChar) {
// encoded as 0x1y2z
patternList.append(map.get(lastChar)).append(lastCnt);
lastCnt = 1;
lastChar = ch;
} else {
++lastCnt;
}
}
// append the last
patternList.append(map.get(lastChar)).append(lastCnt);
return patternList.toString();
}
}