-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_2451.java
34 lines (31 loc) · 1.1 KB
/
_2451.java
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
package com.fishercoder.solutions.thirdthousand;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class _2451 {
public static class Solution1 {
public String oddString(String[] words) {
Map<List<Integer>, List<String>> map = new HashMap<>();
for (String word : words) {
List<Integer> diffs = computeDiff(word);
List<String> list = map.getOrDefault(diffs, new ArrayList<>());
list.add(word);
map.put(diffs, list);
}
for (Map.Entry<List<Integer>, List<String>> entry : map.entrySet()) {
if (entry.getValue().size() == 1) {
return entry.getValue().get(0);
}
}
return null;
}
private List<Integer> computeDiff(String word) {
List<Integer> diffs = new ArrayList<>();
for (int i = 0; i < word.length() - 1; i++) {
diffs.add(word.charAt(i + 1) - word.charAt(i));
}
return diffs;
}
}
}