-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_249.java
More file actions
26 lines (22 loc) · 794 Bytes
/
Copy pathP_249.java
File metadata and controls
26 lines (22 loc) · 794 Bytes
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
package leetcode.medium;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class P_249 {
public List<List<String>> groupStrings(String[] strings) {
final Map<String, List<String>> map = new HashMap<>();
for (String s : strings) {
final int shift = s.charAt(0) - 'a';
final StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
sb.append(getChar(c, shift));
}
map.computeIfAbsent(sb.toString(), v -> new ArrayList<>()).add(s);
}
return new ArrayList<>(map.values());
}
private static char getChar(char c, int shift) {
return (char) (c - shift < 'a' ? c + 26 - shift : c - shift);
}
}