File tree 1 file changed +7
-13
lines changed
1 file changed +7
-13
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public List <String > findAndReplacePattern (String [] words , String pattern ) {
3
- String patternCode = getCode (pattern );
4
- List <String > list = new ArrayList <>();
5
- for (String word : words ) {
6
- String wordCode = getCode (word );
7
- if (wordCode .equals (patternCode )) {
8
- list .add (word );
9
- }
10
- }
11
- return list ;
3
+ String patternString = getPattern (pattern );
4
+ return Arrays .stream (words ).filter (e -> getPattern (e ).equals (patternString ))
5
+ .collect (Collectors .toList ());
12
6
}
13
7
14
- private String getCode (String word ) {
15
- Map <Character , Integer > map = new HashMap <>();
8
+ private String getPattern (String s ) {
16
9
StringBuilder sb = new StringBuilder ();
17
- int count = 0 ;
18
- for (char c : word .toCharArray ()) {
10
+ Map <Character , Integer > map = new HashMap <>();
11
+ int count = 1 ;
12
+ for (char c : s .toCharArray ()) {
19
13
if (!map .containsKey (c )) {
20
14
map .put (c , count ++);
21
15
}
You can’t perform that action at this time.
0 commit comments