finding if two words are anagrams of each other

后端 未结 22 1234
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 14:51

I am looking for a method to find if two strings are anagrams of one another.

Ex: string1 - abcde
string2 - abced
Ans = true
Ex: string1 - abcde
string2 - ab         


        
22条回答
  •  一生所求
    2020-11-27 15:15

    Using an ASCII hash-map that allows O(1) look-up for each char.

    The java example listed above is converting to lower-case that seems incomplete. I have an example in C that simply initializes a hash-map array for ASCII values to '-1'

    If string2 is different in length than string 1, no anagrams

    Else, we update the appropriate hash-map values to 0 for each char in string1 and string2

    Then for each char in string1, we update the count in hash-map. Similarily, we decrement the value of the count for each char in string2.

    The result should have values set to 0 for each char if they are anagrams. if not, some positive value set by string1 remains

    #include 
    #include 
    #include 
    
    #define ARRAYMAX 128
    
    #define True        1
    #define False       0
    
    int isAnagram(const char *string1, 
                const char *string2) {
    
        int str1len = strlen(string1);
        int str2len = strlen(string2);
    
        if (str1len != str2len) /* Simple string length test */
            return False;
    
        int * ascii_hashtbl = (int * ) malloc((sizeof(int) * ARRAYMAX));
        if (ascii_hashtbl == NULL) {
            fprintf(stderr, "Memory allocation failed\n");
            return -1;
        }
        memset((void *)ascii_hashtbl, -1, sizeof(int) * ARRAYMAX);
        int index = 0;
        while (index < str1len) { /* Populate hash_table for each ASCII value 
                                    in string1*/
            ascii_hashtbl[(int)string1[index]] = 0;
            ascii_hashtbl[(int)string2[index]] = 0;
            index++;
        }
        index = index - 1;
        while (index >= 0) {
            ascii_hashtbl[(int)string1[index]]++; /* Increment something */
            ascii_hashtbl[(int)string2[index]]--; /* Decrement something */
            index--;
        }
        /* Use hash_table to compare string2 */
        index = 0;
        while (index < str1len) {
            if (ascii_hashtbl[(int)string1[index]] != 0) {
                /* some char is missing in string2 from string1 */
                free(ascii_hashtbl);
                ascii_hashtbl = NULL;
                return False;
            }
            index++;
        }
        free(ascii_hashtbl);
        ascii_hashtbl = NULL;
        return True;
    }
    
    int main () {
        char array1[ARRAYMAX], array2[ARRAYMAX];
        int flag;
    
        printf("Enter the string\n");
        fgets(array1, ARRAYMAX, stdin);
        printf("Enter another string\n");
        fgets(array2, ARRAYMAX, stdin);
    
        array1[strcspn(array1, "\r\n")] = 0;
        array2[strcspn(array2, "\r\n")] = 0;
        flag = isAnagram(array1, array2);
        if (flag == 1)
            printf("%s and %s are anagrams.\n", array1, array2);
        else if (flag == 0)
            printf("%s and %s are not anagrams.\n", array1, array2);
    
        return 0;
    }
    

提交回复
热议问题