5ms, concise and easy to understand Java solution.


  • 0
    X
     public List<List<String>> groupStringsMy(String[] strings) {
        Map<String, List<String>> pattern = new HashMap<>();
        for(String s:strings){
            StringBuilder p = new StringBuilder();
            for(int i = 1; i<s.length();i++){
                int d = s.charAt(i) - s.charAt(i-1);
                d = d < 0 ? 26 + d : d;
                p.append(d);
            }
            if(!pattern.containsKey(p.toString())){
                pattern.put(p.toString(), new ArrayList<>());
            }
            pattern.get(p.toString()).add(s);
        }
        return new ArrayList<List<String>>(pattern.values());
    }
    

Log in to reply
 

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