forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroupShiftedStrings.java
36 lines (26 loc) · 971 Bytes
/
GroupShiftedStrings.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
package easy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GroupShiftedStrings {
public List<List<String>> groupStrings(String[] strings) {
List<List<String>> result = new ArrayList<List<String>>();
Map<String, List<String>> map = new HashMap<String, List<String>>();
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<String>());
map.get(key).add(word);
}
for(List<String> list : map.values()){
Collections.sort(list);
result.add(list);
}
return result;
}
}