HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Given an array of Strings, return the number of sets of anagrams

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thenumberanagramsarrayreturnsetsstringsgiven

Problem

Please review my code:

public class Anagram {

    public static void main(String args[]) {
        String[] arr = { "abc", "cbc", "bcc", "dog", "god", "mary", "army",
                "rty" };
        HashMap> mapList = new HashMap>();

        for (int i = 0; i  list = new ArrayList();
                mapList.put(hashcode, list);
                list.add(arr[i]);
            } else {
                List list = mapList.get(hashcode);
                list.add(arr[i]);
            }
        }

        printMap(mapList);

        System.out.println("Count=" + mapList.size());
    }

    private static void printMap(HashMap> mapList) {

        if (mapList != null && mapList.size() > 0) {
            for (Integer key : mapList.keySet()) {
                List list = mapList.get(key);

                System.out.print("[");
                if (list != null && list.size() > 0) {
                    for (int k = 0; k < list.size(); k++) {
                        System.out.print(list.get(k) + " ");
                    }
                }
                System.out.print("]");
            }
        }

    }

    private static int gethashcode(String str) {
        int hashcode = 0;
        char ch[] = str.toCharArray();

        for (int i = 0; i < ch.length; i++) {
            if (hashcode != 0) {
                hashcode = hashcode + String.valueOf(ch[i]).hashCode();
            } else {
                hashcode = String.valueOf(ch[i]).hashCode();
            }
        }
        return hashcode;
    }
}


Output:

[mary army ][abc ][rty ][cbc bcc ][dog god ]    Count=5

Solution

Bug

I fed "aad" and "abc" to your program, and it considered the two strings anagrams of each other. The problem is that you are trying to use a hashcode to determine whether two strings are anagrams of each other. However:

-
Your hashcode is not very good because it simply adds the characters of the string together. Therefore "aad" and "abc" were considered equivalent.

-
No matter what, a 32 bit hashcode is going to have some problems because there are more than 2^32 different anagram possibilities if you use long enough strings. Even a 64 bit hashcode would not be enough.

I suggest that you think about creating a sorted string (i.e. alphabetized) as your hash key instead of a numerical hashcode.

Simplification

This code:

if (mapList != null && mapList.get(hashcode) == null) {
            List list = new ArrayList();
            mapList.put(hashcode, list);
            list.add(arr[i]);
        } else {
            List list = mapList.get(hashcode);
            list.add(arr[i]);
        }


could be simplified to:

List list = mapList.get(hashcode);

        if (list == null) {
            list = new ArrayList();
            mapList.put(hashcode, list);
        }
        list.add(arr[i]);

Code Snippets

if (mapList != null && mapList.get(hashcode) == null) {
            List<String> list = new ArrayList<String>();
            mapList.put(hashcode, list);
            list.add(arr[i]);
        } else {
            List<String> list = mapList.get(hashcode);
            list.add(arr[i]);
        }
List<String> list = mapList.get(hashcode);

        if (list == null) {
            list = new ArrayList<String>();
            mapList.put(hashcode, list);
        }
        list.add(arr[i]);

Context

StackExchange Code Review Q#95530, answer score: 3

Revisions (0)

No revisions yet.