-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0890.java
More file actions
58 lines (51 loc) · 2.06 KB
/
_0890.java
File metadata and controls
58 lines (51 loc) · 2.06 KB
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
54
55
56
57
58
package com.github.aditya;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class _0890 {
public static class Solution {
public List<String> findAndReplacePattern(String[] words, String pattern) {
Map<Character, ArrayList<Integer>> patternMap = returnHashMap(pattern);
List<String> solution = new ArrayList<>();
for (int i = 0; i < words.length; i++) {
Map<Character, ArrayList<Integer>> wordMap = returnHashMap(words[i]);
int count = 0;
if (wordMap.size() != patternMap.size()) {
continue;
}
for (int j = 0; j < wordMap.size(); j++) {
if (wordMap.get(words[i].charAt(j)).equals(patternMap.get(pattern.charAt(j)))) {
count++;
} else {
break;
}
}
if (count == wordMap.size()) {
solution.add(words[i]);
}
}
return solution;
}
public Map<Character, ArrayList<Integer>> returnHashMap(String inputWord) {
Map<Character, ArrayList<Integer>> hashMap = new HashMap<>();
for (int i = 0; i < inputWord.length(); i++) {
ArrayList<Integer> positionList;
if (hashMap.containsKey(inputWord.charAt(i))) {
positionList = hashMap.get(inputWord.charAt(i));
} else {
positionList = new ArrayList<>();
}
positionList.add(i);
hashMap.put(inputWord.charAt(i), positionList);
}
return hashMap;
}
public static void main(String[] args) {
String[] words = {"abcba", "deqeq", "meeea", "aqqqa", "dkdde", "cccca"};
Solution sol = new Solution();
List<String> solution = sol.findAndReplacePattern(words, "abbba");
System.out.println(solution);
}
}
}