package com.fishercoder.solutions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**Given a string, we can "shift" each of its letter to its successive letter, for example: "abc" -> "bcd". We can keep "shifting" which forms the sequence:

 "abc" -> "bcd" -> ... -> "xyz"
 Given a list of strings which contains only lowercase alphabets, group all strings that belong to the same shifting sequence.

 For example, given: ["abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"],
 A solution is:

 [
 ["abc","bcd","xyz"],
 ["az","ba"],
 ["acef"],
 ["a","z"]
 ]

 */
public class _249 {

    public List<List<String>> groupStrings(String[] strings) {

        List<List<String>> result = new ArrayList<List<String>>();
        Map<String, List<String>> map = new HashMap<String, List<String>>();

        for (String word : strings) {
            String key = "";
            int offset = word.charAt(0) - 'a';
            for (int i = 1; i < word.length(); i++) {
                key += (word.charAt(i) - offset + 26) % 26;
            }

            if (!map.containsKey(key)) {
                map.put(key, new ArrayList<>());
            }
            map.get(key).add(word);
        }

        for (List<String> list : map.values()) {
            Collections.sort(list);
            result.add(list);
        }

        return result;

    }

}

@@ -162,507 +162,70 @@
 ap;%0A
-/**Given a string, we can %22shift%22 each of its letter to its successive letter, for example: %22abc%22 -%3E %22bcd%22. We can keep %22shifting%22 which forms the sequence:%0A%0A %22abc%22 -%3E %22bcd%22 -%3E ... -%3E %22xyz%22%0A Given a list of strings which contains only lowercase alphabets, group all strings that belong to the same shifting sequence.%0A%0A For example, given: %5B%22abc%22, %22bcd%22, %22acef%22, %22xyz%22, %22az%22, %22ba%22, %22a%22, %22z%22%5D,%0A A solution is:%0A%0A %5B%0A %5B%22abc%22,%22bcd%22,%22xyz%22%5D,%0A %5B%22az%22,%22ba%22%5D,%0A %5B%22acef%22%5D,%0A %5B%22a%22,%22z%22%5D%0A %5D%0A%0A */%0Apublic class _249 %7B%0A%0A
+%0Apublic class _249 %7B%0A%0A    public static class Solution1 %7B%0A    
     
@@ -280,24 +280,28 @@
 strings) %7B%0A%0A
+    
         List
@@ -363,16 +363,20 @@
         
+    
 Map%3CStri
@@ -437,16 +437,20 @@
 g%3E%3E();%0A%0A
+    
         
@@ -491,16 +491,20 @@
         
+    
 String k
@@ -512,16 +512,20 @@
 y = %22%22;%0A
+    
         
@@ -571,24 +571,28 @@
             
+ 
+   
 for (int i =
@@ -617,24 +617,28 @@
 h(); i++) %7B%0A
+    
             
@@ -689,35 +689,43 @@
 26;%0A            
+  
+  
 %7D%0A%0A
+    
             if (
@@ -761,24 +761,28 @@
             
+   
+ 
 map.put(key,
@@ -814,18 +814,25 @@
         
-%7D%0A
+    %7D%0A   
         
@@ -831,24 +831,25 @@
             
+ 
 map.get(key)
@@ -860,35 +860,43 @@
 (word);%0A        
-%7D%0A%0A
+    %7D%0A%0A    
         for (Lis
@@ -940,16 +940,20 @@
         
+    
 Collecti
@@ -960,32 +960,36 @@
 ons.sort(list);%0A
+    
             resu
@@ -1002,35 +1002,43 @@
 (list);%0A        
-%7D%0A%0A
+    %7D%0A%0A    
         return r
@@ -1044,16 +1044,25 @@
 result;%0A
+        %7D
 %0A    %7D%0A%0A

refactor 249