Java O(n*k) solution using xor.
Intuition\n Describe your first thoughts on how to solve this problem. \nWe have to combine the strings into list of same values and keep track of each list. So
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe have to combine the strings into list of same values and keep track of each list. So the simple solution is to create a HashMap< String, List >.\n\n# Approach 1\n<!-- Describe your approach to solving the problem. -->\nNow the main thing is to find a way to keep Key maintained and same for similar anagrams. One way is to do sorting of the elements.\nLike -> \n1. ate -> [\'a\',\'t\',\'e\'] -> after sorting and combining -> aet.\n2. tae -> [\'t\',\'a\',\'e\'] -> after sorting and combining -> aet.\n\n# Complexity\n- Time complexity: O(n * k * log(k) ), where\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- - n = length of array\n- - k = length of string\n\n- Space complexity: O( n ) , size of map\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nimport java.util.*;\nclass Solution {\n    public List<List<String>> groupAnagrams(String[] strs) {\n        \n        Map<String, ArrayList<String>> map = new HashMap<>();\n\n        for( String s: strs ){\n\n            char[] valArr = s.toCharArray();\n            Arrays.sort( valArr );\n            String key = new String(valArr);\n\n            ArrayList<String> ll = map.getOrDefault( key,\n                                     new ArrayList<String>() );\n            ll.add(s);\n            map.put( key, ll );\n                                 \n        }\n\n        List<List<String>> ans = new ArrayList<>();\n        for( Map.Entry<String, ArrayList<String>> entry: map.entrySet() ){\n            ans.add( entry.getValue() );\n        }\n\n        return ans;\n    }\n}\n```\n\n# Approach 2\n<!-- Describe your approach to solving the problem. -->\nNow we can optimise the Key identifying procedure by using Xor and Product of ASCII values of character in Strings. As sorting require extra k*log(k) time and we can reduce it to O(k) for linear traversal.\n\n# Complexity\n- Time complexity: O(n * k), where\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- - n = length of array\n- - k = length of string\n\n- Space complexity: O( n ) , size of map\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nimport java.util.*;\nclass Solution {\n    public List<List<String>> groupAnagrams(String[] strs) {\n        \n        Map<String, ArrayList<String>> map = new HashMap<>();\n\n        for( String s: strs ){\n\n            String key = getKey(s);\n\n            ArrayList<String> ll = map.getOrDefault( key,\n                                     new ArrayList<String>() );\n            ll.add(s);\n            map.put( key, ll );\n                                 \n        }\n\n        List<List<String>> ans = new ArrayList<>();\n        for( Map.Entry<String, ArrayList<String>> entry: map.entrySet() ){\n            ans.add( entry.getValue() );\n        }\n\n        return ans;\n    }\n\n    private String getKey(String s){\n        int prod = 1, xor = 0;\n        for(int len=0; len<s.length(); len++){\n            char i = s.charAt(len);\n            prod *= i;\n            xor = xor^i;\n        }\n        return String.valueOf(prod) + \'_\' + String.valueOf(xor);\n    }\n\n}\n```\n\n![upvote.jpeg](https://assets.leetcode.com/users/images/70fb8114-54fb-46bb-8481-e676441046c7_1676881723.60565.jpeg)\n\n
['Java']