Group Shifted Strings
Last updated
Last updated
class Solution {
public List<List<String>> groupStrings(String[] strings) {
List<List<String>> res = new ArrayList<>();
Map<String, List<String>> rules = new HashMap<>();
for (String str : strings) {
StringBuilder key = new StringBuilder();
for (int i = 1; i < str.length(); i++) {
int x = str.charAt(i) - str.charAt(i - 1);
key.append(str.charAt(i) < str.charAt(i - 1) ? x + 26 : x);
}
if (!rules.containsKey(key.toString())) {
rules.put(key.toString(), new ArrayList<>());
}
rules.get(key.toString()).add(str);
}
for (String key : rules.keySet()) {
res.add(rules.get(key));
}
return res;
}
}