File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
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
+ ```
You can’t perform that action at this time.
0 commit comments