Use customized hash function


  • 0
    B
    public List<List<String>> groupStrings(String[] strings) {
            HashMap<String, List<String>> map = new HashMap<>();
            for(String string : strings) {
                String hashed = hashString(string);
                List<String> list = map.getOrDefault(hashed, new ArrayList<>());
                list.add(string);
                map.put(hashed, list);
            }
            List<List<String>> ret = new ArrayList<>();
            for(String key : map.keySet()) {
                ret.add(map.get(key));
            }
            return ret;
        }
        public String hashString(String string) {
            String ret = "";
            for(int i = 0; i < string.length(); i++) {
                if(i == 0) ret+= "0,";
                else ret += (string.charAt(i) - string.charAt(0) + 26) % 26 + ",";        
            }
            return ret;
        }
    

Log in to reply
 

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