My code:
public class Solution {
public List<List<String>> groupStrings(String[] strings) {
List<List<String>> ret = new ArrayList<List<String>>();
if (strings == null || strings.length == 0) {
return ret;
}
HashMap<String, List<String>> map = new HashMap<String, List<String>>();
for (int i = 0; i < strings.length; i++) {
String s = strings[i];
int offset = s.charAt(0) - 'a';
StringBuilder sb = new StringBuilder();
for (int j = 0; j < s.length(); j++) {
char c = (char) (s.charAt(j) - offset);
if (c < 'a') {
c = (char) (c + 26);
}
sb.append(c);
}
String shiftedStr = sb.toString();
if (!map.containsKey(shiftedStr)) {
map.put(shiftedStr, new ArrayList<String>());
}
map.get(shiftedStr).add(s);
}
for (String head : map.keySet()) {
ret.add(map.get(head));
}
return ret;
}
}
reference:
https://discuss.leetcode.com/topic/20722/my-concise-java-solution
這道題目也是沒做出來》茫現(xiàn)在想想维雇,思路還是很清晰的。
就是把所有的string全部轉換到 'a'開頭鹃骂,然后hashtable 解決秸侣。
好久沒做string存筏,有些生疏。
Anyway, Good luck, Richardo! -- 09/18/2016