Skip to content

Commit 8940abe

Browse files
authored
Create Tony the Cyclist.md
1 parent 44324ae commit 8940abe

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
```
2+
import java.util.*;
3+
class Solution {
4+
public List<String> findAndReplacePattern(String[] words, String pattern) {
5+
List<String> result = new ArrayList();
6+
String p = transfor(pattern);
7+
String w;
8+
String ww;
9+
for (int i = 0; i < words.length; i++){
10+
w = words[i];
11+
ww = transfor(w);
12+
if (ww.equals(p)){
13+
result.add(w);
14+
}
15+
16+
}
17+
return result;
18+
}
19+
20+
private String transfor(String s){
21+
Map<Character, Character> m = new HashMap<>();
22+
char j = 0;
23+
String ss = "";
24+
m.put(s.charAt(0), (char)(j + '0'));
25+
for(int i = 1; i < s.length(); i++){
26+
if (s.charAt(i-1) != s.charAt(i)){
27+
if (m.containsKey(s.charAt(i))){
28+
ss += m.get(s.charAt(i));
29+
}
30+
else {
31+
++j;
32+
ss += (char)(j+'0');
33+
m.put(s.charAt(i), (char)(j + '0'));
34+
}
35+
}
36+
}
37+
return ss;
38+
}
39+
}
40+
```

0 commit comments

Comments
 (0)