[LeetCode]Group Shifted Strings
public class Solution { public List<List<String>> groupStrings(String[] strings) { HashMap<String, List<String>> map = new HashMap<String, List<String>>(); for (String str :strings) { String tmp = helper(str); List<String> list = map.containsKey(tmp) ? map.get(tmp) : new ArrayList<String>(); list.add(str); map.put(tmp, list); } Iterator<String> it = map.keySet().iterator(); List<List<String>> result = new ArrayList<List<String>>(); while (it.hasNext()) { List<String> ll = map.get(it.next()); Collections.sort(ll); result.add(ll); } return result; } private String helper(String str) { char[] chars = str.toCharArray(); int tmp = (int)chars[0] - (int)'0'; String result = String.valueOf(0) + ","; for (int i = 1; i < chars.length; i++) { int num = (int)chars[i] - (int)'0'; num = num - tmp; num += num > 0 ? 0 : 26; result = result + String.valueOf(num) + ","; } return result; } }