My Simple 7ms Java Solution


  • 0
    F
    public List<List<String>> groupStrings(String[] strings) {
    	HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
    	for (String s : strings) {
    		StringBuilder keyTemp = new StringBuilder();
    		String key;
    		for (int i = 1; i < s.length(); i++)
    			keyTemp.append((s.charAt(i) - s.charAt(i - 1) + 26) % 26);
    		key = keyTemp.toString();
    		if (!map.containsKey(key))
    			map.put(key.toString(), new ArrayList<String>());
    		map.get(key.toString()).add(s);
    	}
    	return new ArrayList<List<String>>(map.values());
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.