In this Leetcode Group Anagrams problem solution, we have given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Problem solution in Python.
class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: anagrams = {} for s in strs: s_sorted = "".join(sorted(s)) if s_sorted not in anagrams: anagrams[s_sorted] = [] anagrams[s_sorted].append(s) return [list(value) for key, value in anagrams.items()]
Problem solution in Java.
class Solution { public List<List<String>> groupAnagrams(String[] strs) { List<List<String>> result = new ArrayList<List<String>>(); Map<String, List<String>> groups = new HashMap<String, List<String>>(); for (int i = 0; i < strs.length; i++){ String str = strs[i]; String key = buildAnagramsKey(str); if (!groups.containsKey(key)){ groups.put(key, new ArrayList<String>()); } groups.get(key).add(str); } for (Map.Entry<String, List<String>> pair : groups.entrySet()){ result.add(pair.getValue()); } return result; } public String buildAnagramsKey(String str){ int[] map = new int[26]; for (Character c : str.toCharArray()){ map[c - 'a'] += 1; } StringBuilder build = new StringBuilder(); for (int i = 0; i < 26; i++){ build.append(map[i]); build.append((char) (i + 'a')); } return build.toString(); } }
Problem solution in C++.
class Solution { public: vector<vector<string>> groupAnagrams(vector<string>& strs) { unordered_map<string, vector<string>> m; vector<vector<string>> ans; for(int i=0;i<strs.size();i++){ string p = strs[i]; sort(p.begin(), p.end()); m[p].push_back(strs[i]); } for(auto it= m.begin();it!=m.end();it++){ ans.push_back(it->second); } return ans; } };
Problem solution in C.
struct Node{ int key; char *data; struct Node *next; }; int checkAnagram(char *str[],int length){ int i=0; size_t size; int sumAllChar=0; struct Node *newNode; struct Node *array=(struct node*)(malloc(length*sizeof(struct Node))); for(i=0;i<length;i++){ array[i].key=-1; array[i].data=NULL; array[i].next=NULL; } for(i=0;i<length;i++){ sumAllChar = sumOfAllCharacters(str[i]); struct Node *existsNode = checkKeyExists(array,sumAllChar,length); if(existsNode == NULL) { array[i].key = sumAllChar; array[i].data = str[i]; } else{ newNode = createNewNode(sumAllChar,str[i]); struct Node *temp = existsNode; while(temp && temp->next!=NULL){ temp = temp->next; } temp->next=newNode; } } for(i=0;i<length;i++){ if(array[i].key != -1) { printf("n%d - %s", array[i].key, array[i].data); struct Node *temp = array[i].next; while (temp) { printf(",%s", temp->data); temp = temp->next; } } } } struct Node *checkKeyExists(struct Node *array,int key,int length){ int i=0; for(i=0;i<length;i++){ if(array[i].key==key){ return &array[i]; } } return NULL; } struct Node* createNewNode(int number,char *data){ struct Node *newNode = (struct Node *)malloc(sizeof(struct Node)); newNode->key = number; newNode->data=data; newNode->next = NULL; return newNode; } int sumOfAllCharacters(char *str){ int i=0; int sumAllChar = 0; int len = strlen(str); for(i=0;i<len;i++){ int charNumber = str[i]; sumAllChar+=charNumber; } return sumAllChar; }