classSolution {publicList<List<String>> groupStrings(String[] strings) {List<List<String>> res =newArrayList<List<String>>();Map<Integer,List<List<String>>> map =newHashMap<Integer,List<List<String>>>();// group by lenfor (String s : strings) {int len =s.length();if (!map.containsKey(len)){map.put(len,newArrayList<List<String>>());List<String> tmpList =newArrayList<String>();;tmpList.add(s);map.get(len).add(tmpList); } else {boolean added =false;for (List<String> list :map.get(len)) {if (same(list.get(0), s)) {list.add(s); added =true;break; } }if (!added) {List<String> tmpList =newArrayList<String>();tmpList.add(s);map.get(len).add(tmpList); } } }// add to resultfor (List<List<String>> lists :map.values()) {for (List<String> list : lists) res.add(list); }return res; }privatebooleansame(String s1,String s2) {int diff = (s2.charAt(0) -s1.charAt(0) +26) %26;for (int i =1; i <s1.length(); i++) {int num1 =s1.charAt(i) -'a';int num2 =s2.charAt(i) -'a';if ( (num1 + diff) %26!= num2 ) returnfalse; }returntrue; }}
ac2: transform every word back to base status
classSolution {publicList<List<String>> groupStrings(String[] strings) {List<List<String>> res =newArrayList<List<String>>();Map<String,List<String>> map =newHashMap<String,List<String>>();for (String s : strings) {// get base keyString key ="";int diff =s.charAt(0) -'a';for (int i =0; i <s.length(); i++) {char c = (char) (s.charAt(i) - diff);if (c <'a') c +=26; key += c; }// add to mapif (!map.containsKey(key)) map.put(key,newArrayList<String>());map.get(key).add(s); }// add to result returnfor (List<String> list :map.values()) {res.add(list); }return res; }}