3ms Easy to understand JAVA solution


  • 0
    J
    class Solution 
    {
        public List<List<String>> groupStrings(String[] strings) 
        {
            Map<String, List<String>> map = new HashMap<>();
            for (String s : strings)
            {
                char[] arr = s.toCharArray();
                int offset = arr[0] - 'a';
                for (int i = 0; i < arr.length; i++)
                {
                    arr[i] = (char) (arr[i] - offset);
                    if (arr[i] < 'a') arr[i] = (char) ('z' - ('a' - arr[i] - 1));
                }
                String newStr = String.valueOf(arr);
                if (!map.containsKey(newStr)) map.put(newStr, new ArrayList<String>());
                map.get(newStr).add(s);
            }
            
            return new ArrayList(map.values());
        }
    }
    

Log in to reply
 

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