LeetCode: Group Shifted Strings

LeetCode: Group Shifted Strings

Given a string, we can “shift” each of its letter to its successive letter, for example: “abc” -> “bcd”. We can keep “shifting” which forms the sequence:

1
"abc" -> "bcd" -> ... -> "xyz"

Given a list of strings which contains only lowercase alphabets, group all strings that belong to the same shifting sequence.

For example,

given: ["abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"], Return:

1
2
3
4
5
6
[
["abc","bcd","xyz"],
["az","ba"],
["acef"],
["a","z"]
]

Note: For the return value, each inner list’s elements must follow the lexicographic order.

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
public class Solution {
public List<List<String>> groupStrings(String[] strings) {

assert strings != null : "null array";

List<List<String>> result = new ArrayList<>();
Map<String, List<String>> map = new HashMap<>();

for (String s : strings) {
String code = getFeatureCode(s);
List<String> val;
if (!map.containsKey(code)) {
val = new ArrayList<>();
} else {
val = map.get(code);
}
val.add(s);
map.put(code, val);
}

for (String key : map.keySet()) {
List<String> val = map.get(key);
Collections.sort(val);
result.add(val);
}

return result;
}

private String getFeatureCode(String s) {
StringBuilder sb = new StringBuilder();
sb.append("#");
for (int i = 1; i < s.length(); i++) {
int tmp = ((s.charAt(i) - s.charAt(i - 1)) + 26) % 26;
sb.append(tmp).append("#");
}
return sb.toString();
}
}