Created
June 13, 2018 05:32
-
-
Save bilbo3000/c3112c81a2d5e332832cf06829fabdfa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public List<List<String>> groupStrings(String[] list) { | |
Map<String, List<String>> m = new HashMap<>(); | |
for (String s : list) { | |
String key = convert(s); | |
if (!m.containsKey(key)) { | |
m.put(key, new ArrayList<>()); | |
} | |
m.get(key).add(s); | |
} | |
List<List<String>> result = new ArrayList<>(); | |
for (String key : m.keySet()) { | |
result.add(m.get(key)); | |
} | |
return result; | |
} | |
private String convert(String s) { | |
String res = ""; | |
char[] arr = s.toCharArray(); | |
for (int i = 1; i < arr.length; i++) { | |
if (arr[i] >= arr[i - 1]) { | |
res += String.valueOf((int)(arr[i] - arr[i - 1])); | |
} else { | |
res += String.valueOf((int)(26 + arr[i] - arr[i - 1]));; | |
} | |
} | |
return res; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment