Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java We define an anagram to be a word whose characters can be rearranged to

ID: 2247728 • Letter: I

Question

In Java

We define an anagram to be a word whose characters can be rearranged to create another word. Given two strings, we want to know the minimum number of characters already in either string that we must modify to make the two strings anagrams: if it's not possible to make the two strings anagrams, we consider this number to be -1. For example: tea and ate are anagrams, so we would need to modify a minimum of 0 characters. tea and toe are not anagrams, but we can modify a minimum of 1 character in either string to make them anagrams. act and acts are not anagrams and cannot be converted to anagrams because they contain different numbers of characters, so the minimum number of characters to modify is -1. Complete the function in the editor below. It has two parameters: 1. An array of n strings, a. 2. An array of n strings, b. The function must return an array of integers where each element i denotes the minimum number of characters you must modify to make a_i and b_i anagrams: if it's not possible to modify the existing characters in a_i and b_i to make them anagrams, element i should be -1 instead. The first line contains an integer, n, denoting the number of elements in a. Each line i of the n subsequent lines contains a string describing a_i. The next line contains an integer, n, denoting the number of elements in b. Each line i of the n subsequent lines contains a string describing b_i. Each string consists of lowercase English alphabetic letters (i.e., a to z). 1 lessthanorequalto n lessthanorequalto 100 It is guaranteed that a and b contain the same number of elements. 0 lessthanorequalto length of a_i, length of b_i lessthanorequalto 10^4

Explanation / Answer

public class AnagramProgram

{

    static void isAnagram(String s1, String s2)

    {

        //Removing white spaces from s1 and s2 and changing case to lower

        String copyOfs1 = s1.replaceAll("\s", "").toLowerCase();

        String copyOfs2 = s2.replaceAll("\s", "").toLowerCase();

        //Initially setting status as true

        boolean status = true;

        if(copyOfs1.length() != copyOfs2.length())

        {

            //Setting status as false if copyOfs1 and copyOfs2 doesn't have same length

            status = false;

        }

        else

        {

            //Converting copyOfs1 to char array

            char[] s1ToArray = copyOfs1.toCharArray();

            //Checking whether each character of s1ToArray is present in copyOfs2

            for (char c : s1ToArray)

            {

                int index = copyOfs2.indexOf(c);

                if(index != -1)

                {

                    //If character is present in copyOfs2, removing that char from copyOfs2

                    copyOfs2 = copyOfs2.substring(0, index)+copyOfs2.substring(index+1, copyOfs2.length());

                }

                else

                {

                    //If character is not present in copyOfs2, setting status as false and breaking the loop

                    status = false;

                    break;

                }

            }

        }

        //Output

        if(status)

        {

            System.out.println(s1+" and "+s2+" are anagrams");

        }

        else

        {

            System.out.println(s1+" and "+s2+" are not anagrams");

        }

    }

    public static void main(String[] args)

    {

        isAnagram("Mother In Law", "Hitler Woman");

        isAnagram("keEp", "peeK");

        isAnagram("SiLeNt CAT", "LisTen AcT");

        isAnagram("Debit Card", "Bad Credit");

        isAnagram("School MASTER", "The ClassROOM");

        isAnagram("DORMITORY", "Dirty Room");

        isAnagram("ASTRONOMERS", "NO MORE STARS");

        isAnagram("Toss", "Shot");

        isAnagram("joy", "enjoy");

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote