Simple Java Solution


  • 0
    M
    public class Solution {
        public List<List<String>> groupStrings(String[] strings) {
            List<List<String>> lists = new ArrayList<>();
    		if (strings == null || strings.length == 0) return lists;
    		Map<String, List<String>> groups = new HashMap<>();
    		for (String s : strings) {
    			String key = getKey(s);
    			if (!groups.containsKey(key)) groups.put(key, new ArrayList<String>());
    			groups.get(key).add(s);
    		}
    		lists = new ArrayList<>(groups.values());
    		return lists;
        }
    	
    	private String getKey(String s) {
    		StringBuilder sb = new StringBuilder();
    		for (int i = 0; i < s.length() - 1; ++i) {
    			int d = s.charAt(i + 1) - s.charAt(i);
    			if (d < 0) d += 26;
    			sb.append(d);
    		}
    		return sb.toString();
    	}
    }
    

Log in to reply
 

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