Group Anagrams - Easiest Explanation You'll See 👩‍💻💡👨‍💻
Basic Concept\n Describe yo0ur first thoughts on how to solve this problem. \nWhat are Anagrams ?\n\nAnagrams are group of words having the same alphabets but ,
# Basic Concept\n<!-- Describe yo0ur first thoughts on how to solve this problem. -->\n**What are Anagrams ?**\n\nAnagrams are group of words having the same alphabets but , arranged in a differnt order. \nfor example : **Eat** and **Ate** , they both have the same alphabets \n`e , a, t` but these characters are organized in different order.\n\nNow , we have to group these anagrams together . \n\n---\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nThe approach is very basic . We are going to sort the strings according to their lexicographical order , and then store these as keys in a hashmap.\n\nIf two strings are anagrams of each other , they must have the same sorted order of characters . \n\nFor example in `Ate` and `Eat` , Ate can be sorted as `A,e,t` and Eat will be sorted as `A,e,t` , which is why these two strings are anagrams of each other. \n\n**Go through the code walkThrough section to get a clearer idea** .\n\n---\n# Code WalkThrough\n\nFirst of all , we declare a `map` that is going to hold all the sorted versions of the strings .\nNow , we iterate through the string in the string array `strs` and convert each string into chararcter array .\n\nFor example in  `strs = ["eat","tea","tan","ate","nat","bat"]` , each time we iterate , `String str` holds a new string from the `strs` array.\n\nFor example in the first iteration , `String str = "eat" ` . Let\'s proceed with this string .  \n\n`char[] charArray = str.toCharArray();` will convert this string into an array of characters , **so that the string can be easily sorted**.\n`eat ` will get converted into `[ e , a , t ]` .\n\nAnd then , we sort this array using `Arrays.sort(charArray);`\nso , `[ e , a , t ]` will get sorted to `[ a , e , t ]` .\n\nFinally , we convert this **sorted array of characters** back to a **String** , since we have to deal with a complete string , not characters. \nThis is done through  : `String sorted = new String(charArray);`\n\n\n\n```\n Map<String , List<String>> map = new HashMap<>();\n\n    for(String str : strs)\n      {\n         char[] charArray = str.toCharArray();\n         Arrays.sort(charArray);\n\n\n        String sorted = new String(charArray);\n```\n\nNow , we have to put these sorted strings as **keys** in the map and the unsorted versions of the strings ( the original ones ) as values of the map . \n\nWe keep checking the map , if two strings have the same key ( ie the sorted version) , then they are anagrams of each other.\n\nSince , the key is just **one** simple sorted string , it is simply added as a key in map , but the value ?\n\nThe value **can hold multiple strings**  , since multiple strings which are anagrams of each other will have **the same sorted version** as key , so we need to add the value as **ArrayList** , through `map.put(sorted , new ArrayList<>());`\n\nNow , lets continue with our `aet` string ( the sorted version ) . First we\'ll check if this string is present in the map , if it is not present in the map ( since map is empty initially ) , so the `if` block of the code executes and `aet` is added as a **key** in the map with an empty ArrayList as it\'s value.\n\nThe map now looks like this : `aet:[ ]`.\n\nNow , since `aet` is added as a key , we execute the next line which is `map.get(sorted).add(str);` , str ,which is `eat` is added as a value to the map with key as `aet` \n```\n{\n  "aet": ["eat"]\n}\n\n```\n\nThe next string we get from the `strs` array is `tea` . As done above , the string is first converted into a character array and then it is sorted as `[ a , e , t ]` .\n\nAfter converting it back into a string ie `aet` , we look for it in the map , now the crux is that we already have `aet` as a key in the map `aet:[ ]` , so the if part does not executes .\n\nSince map already has `aet` , This line `map.get(sorted).add(str);` is now executes , and `str` which is `tea`,  gets added into the map with **key as `aet`** . so now the map looks like : \n```\n{\n  "aet": ["eat","tea"]\n}\n```\n\n\n\n```\nif(!map.containsKey(sorted))\n        {\n            map.put(sorted , new ArrayList<>());\n        }\n\n        map.get(sorted).add(str);\n\n      }\n\n    List<List<String>> result = new ArrayList<>(map.values());\n   return result;\n```\nNow , similarly we iterate through all the strings and group them together with their anagrams.\n\nFinally , we create a **List of lists** to store our result .\n\n**Why ?**\n\n**Multiple Groups** : Since there are multiple groups of anagrams, the output contains several lists, each representing a group. The outer list contains these groups, so the structure is a list of lists.\n\n**Map Values** : In the code, the HashMap stores a list of strings as its value for each sorted string (the key). When we extract all the values from the map at the end, these values are lists of anagrams. Collecting these values into a single list results in a list of lists.\n\n```\nList<List<String>> result = new ArrayList<>(map.values());\n   return result;\n```\n\n\n\n---\n\n# Complexity\n- Time complexity : $$O(N\u2217MLogM)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity : $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n---\n\n# Code\n```java []\nclass Solution {\n    public List<List<String>> groupAnagrams(String[] strs) {\n        \n    Map<String , List<String>> map = new HashMap<>();\n\n    for(String str : strs)\n      {\n         char[] charArray = str.toCharArray();\n         Arrays.sort(charArray);\n\n\n        String sorted = new String(charArray);\n\n        if(!map.containsKey(sorted))\n        {\n            map.put(sorted , new ArrayList<>());\n        }\n\n        map.get(sorted).add(str);\n\n      }\n\n    List<List<String>> result = new ArrayList<>(map.values());\n   return result;\n\n    }\n}\n```\n\n---\n\n**Feel free to discuss this problem in the comments section below , and do Upvote if this simple explanation helped you . Ofcourse , there are better approaches to this problem that I have already implemented , but I wanted to start with this basic approach so that people new to coding can do it with ease**\n\n**Happy Coding!!**\n\n
['Array', 'Hash Table', 'String', 'Sorting', 'Java']