forked from jsjtzyy/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC249_GroupShiftedStrings.java
More file actions
37 lines (36 loc) · 1.08 KB
/
LC249_GroupShiftedStrings.java
File metadata and controls
37 lines (36 loc) · 1.08 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
import java.util.*;
public class LC249_GroupShiftedStrings {
public List<List<String>> groupStrings(String[] strings) {
List<List<String>> res = new ArrayList<>();
if(strings == null || strings.length == 0) return res;
HashMap<String, List<String>> map = new HashMap<>();
StringBuilder sb = null;
String gap = null;
List<String> list = null;
int dist = 0;
for(String str : strings){
sb = new StringBuilder("0");
for(int i = 1; i < str.length(); ++i){
dist = str.charAt(i) - str.charAt(i - 1);
dist = (dist + 26) % 26;
if(dist > 9){
sb.append(String.valueOf(dist));
}else{
sb.append("0" + String.valueOf(dist));
}
}
gap = sb.toString();
if(map.containsKey(gap)){
list = map.get(gap);
}else{
list = new ArrayList<>();
}
list.add(str);
map.put(gap, list);
}
for(List<String> tmp : map.values()){
res.add(tmp);
}
return res;
}
}