Skip to content

Commit fd43b15

Browse files
authored
Update Find and Replace Pattern.java
1 parent 4908c18 commit fd43b15

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

Medium/Find and Replace Pattern.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
class Solution {
22
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());
126
}
137

14-
private String getCode(String word) {
15-
Map<Character, Integer> map = new HashMap<>();
8+
private String getPattern(String s) {
169
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()) {
1913
if (!map.containsKey(c)) {
2014
map.put(c, count++);
2115
}

0 commit comments

Comments
 (0)