Leetcode Group Anagrams Problem Solution

In this Leetcode Group Anagrams Problem Solution 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.

Leetcode Group Anagrams Problem Solution


Problem solution in Python.

class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        res=defaultdict(list)
        
        for string in strs:
            count=[0]*26
            for char in string:
                count[ord(char)-ord("a")]+=1
            res[tuple(count)].append(string)
        return res.values()

Problem solution in Java.

public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> lists = new ArrayList<List<String>>();
        if (strs.length == 0) return lists;
        java.util.Arrays.sort(strs);
        HashMap<String, List<String>> map = new HashMap<String, List<String>>();
        for (String str : strs) {
            char[] charArray = str.toCharArray();
            java.util.Arrays.sort(charArray);
            String sorted = new String(charArray);
            if (map.containsKey(sorted)) {
                List<String> list = map.get(sorted);
                list.add(str);
            }
            else {
                List<String> list = new ArrayList<String>();
                list.add(str);
                map.put(sorted, list);
            }
        }
        
        for (List<String> list : map.values()) {
            lists.add(list);
        }
        
        return lists;
    }


Problem solution in C++.

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        vector<vector<string>> res;
        map<vector<int>, vector<string>> m;
        vector<int> alphabet (26, 0);
        
        for(int i = 0; i < strs.size(); ++i)
        {
            for(int k = 0; k < 26; ++k) alphabet[k] = 0;
            for(int j = 0; j < strs[i].length(); ++j)
                alphabet[strs[i][j] - 'a']++;
            
            m[alphabet].push_back(strs[i]);
        }
        
        for(auto it = m.begin(); it != m.end(); it++)
            res.push_back(it->second);
        
        return res;
    }
};


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];
            //printf("%d - %s\n", sumAllChar, *(str + i));
        }
        else{
            newNode = createNewNode(sumAllChar,str[i]);
            struct Node *temp = existsNode;
            while(temp && temp->next!=NULL){
                temp = temp->next;
            }
            temp->next=newNode;
        }
    }
 
    //print result
    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;
}


Post a Comment

0 Comments