forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_249.java
39 lines (30 loc) · 1.01 KB
/
_249.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
35
36
37
38
39
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class _249 {
public static class Solution1 {
public List<List<String>> groupStrings(String[] strings) {
List<List<String>> result = new ArrayList<>();
Map<String, List<String>> map = new HashMap<>();
for (String word : strings) {
String key = "";
int offset = word.charAt(0) - 'a';
for (int i = 1; i < word.length(); i++) {
key += (word.charAt(i) - offset + 26) % 26;
}
if (!map.containsKey(key)) {
map.put(key, new ArrayList<>());
}
map.get(key).add(word);
}
for (List<String> list : map.values()) {
Collections.sort(list);
result.add(list);
}
return result;
}
}
}