public class Solution {
public List<List<String>> groupStrings(String[] strings) {
List<List<String>> res = new LinkedList<>();
if (strings == null || strings.length == 0) {
return res;
}
Map<String, List<String>> map = new HashMap<>();
for (String string: strings) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < string.length(); i++) {
int shift = (string.charAt(i) - string.charAt(0) + 26)%26;
sb.append(shift + " ");
}
String key = sb.toString();
if (map.containsKey(key)) {
map.get(key).add(string);
} else {
List<String> list = new LinkedList<>();
list.add(string);
map.put(key, list);
res.add(list);
}
}
for (List<String> list: res) {
Collections.sort(list);
}
return res;
}
}
Group Shifted Strings
最新推荐文章于 2021-08-07 12:08:26 发布