You will need to complete the two methods in this class Desc
You will need to complete the two methods in this class. Descriptions of what is needed for each method are below. Note that descriptions are written in plain English. You will need to think through each step and write the Java code which accomplishes this.
Algorithm initialize(firstString)
This method already includes code which makes certain all of the letters are in lower case (other chars will be unaffected). and then converts the String to an array ofchar. To complete this method, start by instantiating an ArrayMultiSet. Add everything in charsInString to your ArrayMultiSet instance EXCEPT \"whitespace\" characters. (Whitespace is the name for characters you cannot see, like spaces, tabs, and newlines). Your loop should use Character.isWhitespace() to determine if achar is a whitespace and only add those for which this is false. Once you have added all of the non-whitespace characters, return the ArrayMultiSet object.
Algorithm isAnagram(first, secondString)
Like initialize(), the provided code guarantees letters are lower case and then converts secondString into an array of char. This method needs to return true if the entries in charsInString are identical to the elements first. To find this, loop through the entries in charsInString. Try to remove each entry from first. Continue doing this until you find an entry that was not in first or the loop completes. If you find an entry that was not in first, then return false since they are not anagrams. If the loop completes, return if there are no elements remaining in first (since this means there are no extra letters).
Code:
public class AnagramCalc {
public ArrayMultiSet initialize(String firstString) {
firstString = firstString.toLowerCase();
char[] charsInString = firstString.toCharArray();
public boolean isAnagram(ArrayMultiSet first, String secondString) {
secondString = secondString.toLowerCase();
char[] charsInString = secondString.toCharArray();
}
}
Solution
public boolean isAnagram(ArrayMultiSet first, String secondString) {
secondString = secondString.toLowerCase();
char[] charsInString = secondString.toCharArray();
char[] charsInString1 = first.toCharArray();
for(char ch : charsInString1) //Java For-each Loop ,It works on elements basis not index. It returns element one /by one in the String1 array
{
int indx = charsInString.indexOf(\"\" + ch);
if(indx != -1){
charsInString.deleteCharAt(indx);
}
else
{
return false;
}
}
}
