How to find anagrams and count the most frequent in a string array in Java

H

The first step is to create a function that compares two input strings, if those strings are an anagram the function will return true or if its not it will return false.

Let’s name our function boolean isAnagram(String string1, String string2).

Of course, if we compare those strings as they are we can’t find out if they are anagrams, so there is a need to process a little bit those string in a way that we will be able to say that they are anagrams.
Now a good way to do this is to make the strings in lower case then we will have to convert those strings into characters arrays and last to sort them out.

This way the characters arrays will contain the same characters but in an order, so if str2 is an anagram of str1 they will have the same size and the same characters in the same order.
Take a look at the code below:

static boolean isAnagram(String string1, String string2) {

        string1 = string1.replaceAll("\\s", "").toLowerCase();
        string2 = string2.replaceAll("\\s", "").toLowerCase();
        
        char[] strArr1 = string1.toCharArray();
        char[] strArr2 = string2.toCharArray();

        Arrays.sort(strArr1);
        Arrays.sort(strArr2);

        return (Arrays.equals(strArr1, strArr2));

    }

The second step is to create a function that will make use of the function isAnagram and count the most frequent one, the total anagrams and the number of unique strings in the input String array and return those values in an array. Let’s name this method int[] mostCommonAnagram(String[] strArray).

In this method we are going to compare each string with the rest of the elements of the input string array. If the function isAnagram return true then we have a match to count, also we must keep count for the number of matches for every different anagram, we will use the variable score for that purpose. If another anagram achieves higher score then we will know it.

Here is the code:

  static int[] mostCommonAnagram(String[] strArray) {
        int[] result = new int[3];
        String mostCommon = "";
        int numOfUniCol = 0, numOfMostFr = 0, score = 0, totalmatchings = 0;

        for (int i = 0; i < strArray.length; i++) {
            for (int j = i + 1; j < strArray.length; j++) {
                if (isAnagram(strArray[i], strArray[j])) {
                    System.out.println(strArray[i] + " is anagram of " + strArray[j]);
                    totalmatchings++;
                    score++;
                    if (numOfMostFr < score) {
                        numOfMostFr = score;                       
                        mostCommon = strArray[i];;                      
                        score = 0;
                    }
                }
            }
        }

        numOfUniCol = strArray.length - (totalmatchings+1);
        result[0] = totalmatchings+1;
        result[1] = numOfUniCol;
        result[2] = numOfMostFr+1; 
        char[] sorted = mostCommon.toCharArray();
        Arrays.sort(sorted);
        mostCommon = Arrays.toString(sorted);
        System.out.println("The most frequent string contains: " + mostCommon);
        return result;
    }

Note: At the end i increase the values of totalmatchings and numOfMostFr by 1.

In the main method you can put any number of anagrams in the array as input in the int[] mostCommonAnagram(String []strArray) and display the results.

public static void main(String args[]) {
        String[] test = {"good","dogo","oogd","morning","gnniorm","test"};
        int[] res = mostCommonAnagram(test);

        System.out.println("Total anagrams:        " + res[0]);
        System.out.println("Unique strings:        " + res[1]);
        System.out.println("Most frequent anagram: " + res[2]);

        

    }

When executed it prints the following results:

run:
good is anagram of dogo
good is anagram of oogd
dogo is anagram of oogd
morning is anagram of gnniorm
The most frequent string contains: [d, g, o, o]
Total anagrams: 5
Unique strings: 1
Most frequent anagram: 3

Below you can find the full source code of the example.

public class NewClass {
   
    static boolean isAnagram(String string1, String string2) {

        string1 = string1.replaceAll("\\s", "").toLowerCase();
        string2 = string2.replaceAll("\\s", "").toLowerCase();
        
        char[] strArr1 = string1.toCharArray();
        char[] strArr2 = string2.toCharArray();

        Arrays.sort(strArr1);
        Arrays.sort(strArr2);

        return (Arrays.equals(strArr1, strArr2));

    }
   

    static int[] mostCommonAnagram(String[] strArray) {
        int[] result = new int[3];
        String mostCommon = "";
        int numOfUniCol = 0, numOfMostFr = 0, score = 0, totalmatchings = 0;

        for (int i = 0; i < strArray.length; i++) {
            for (int j = i + 1; j < strArray.length; j++) {
                if (isAnagram(strArray[i], strArray[j])) {
                    System.out.println(strArray[i] + " is anagram of " + strArray[j]);
                    totalmatchings++;                   
                    score++;
                    if (numOfMostFr < score) {
                        numOfMostFr = score;                       
                        mostCommon = strArray[i];;                      
                        score = 0;
                    }
                }
            }
        }

        numOfUniCol = strArray.length - (totalmatchings+1);
        result[0] = totalmatchings+1;
        result[1] = numOfUniCol;
        result[2] = numOfMostFr+1; 
        char[] sorted = mostCommon.toCharArray();
        Arrays.sort(sorted);
        mostCommon = Arrays.toString(sorted);
        System.out.println("The most frequent string contains: " + mostCommon);
        return result;
    }

   
    public static void main(String args[]) {
        String[] test = {"good","dogo","oogd","morning","gnniorm","test"};
        int[] res = mostCommonAnagram(test);

        System.out.println("Total anagrams:        " + res[0]);
        System.out.println("Unique strings:        " + res[1]);
        System.out.println("Most frequent anagram: " + res[2]);

        

    }
}

 

Disclaimer: The present content may not be used for training artificial intelligence or machine learning algorithms. All other uses, including search, entertainment, and commercial use, are permitted.

Categories

Tags