/
githubmirror
/
interviews
Обзор
Документация
Войти
/
githubmirror
/
interviews
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
leetcode/hash-table/GroupShiftedStrings.java
54 строки
2 KB
Kevin Naughton Jr
finish renaming files and directories
27 мар 2018, 19:52
27 мар 2018, 19:52
ec6dfb5
Код
Авторство
О чём код?
// Given a string, we can "shift" each of its letter to its successive letter, for example: "abc" -> "bcd". We can keep "shifting" which forms the sequence: // "abc" -> "bcd" -> ... -> "xyz" // Given a list of strings which contains only lowercase alphabets, group all strings that belong to the same shifting sequence. // For example, given: ["abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"], // A solution is: // [ // ["abc","bcd","xyz"], // ["az","ba"], // ["acef"], // ["a","z"] // ] public class GroupShiftedStrings { public List<List<String>> groupStrings(String[] strings) { List<List<String>> result = new ArrayList<List<String>>(); HashMap<String, List<String>> map = new HashMap<String, List<String>>(); for(String s : strings) { int offset = s.charAt(0) - 'a'; String key = ""; for(int i = 0; i < s.length(); i++) { char current = (char)(s.charAt(i) - offset); if(current < 'a') { current += 26; } key += current; } if(!map.containsKey(key)) { List<String> list = new ArrayList<String>(); map.put(key, list); } map.get(key).add(s); } for(String key : map.keySet()) { List<String> list = map.get(key); Collections.sort(list); result.add(list); } return result; } }