Question: anagramCount: [challenging] This method takes a single parameter and it must return the number distinct case-insensitive anagrams of the given word that have been added

anagramCount: [challenging] This method takes a single parameter and it must return the number distinct case-insensitive anagrams of the given word that have been added to the processor. An anagram consists of exactly the same number of letters as the given word with each character occurring the same number of times. However, identical words are not considered to be anagrams. For instance: "and" is an anagram of "dan" but "nan" is neither an anagram of "naan" nor of nan. Note that because the match must be case-insensitive, Dan is also an anagram of and. However, because the match is case-insensitive, all of Dan, dan and DAN only count as a single match for any of and, AND and And. Also, each anagram is only counted once, regardless of how many times it has been added to the processor via addWord.

My Java code:

public int anagramCount(String word) { int count = 0; int[] wordFreq = new int[26]; word = word.toLowerCase(); for(char ch : word.toCharArray()){ wordFreq[ch-'a']++; } HashSet checked = new HashSet<>(); for(String str : processor.keySet()){ int[] strFreq = new int[26]; String s = str.toLowerCase(); if(checked.contains(s)){ continue; } checked.add(s); for(char ch : s.toCharArray()){ strFreq[ch-'a']++; } boolean isAnagram = true; for(int i=0; i<26; i++){ if(wordFreq[i] != strFreq[i]){ isAnagram = false; break; } } if(isAnagram){ count++; } } return count; }

Main code

public class TextProcessor { private HashMapprocessor; /** * Constructor for objects of class TextProcessor */ public TextProcessor() { processor = new HashMap<>(); } /** * Add a word to the processor. * @param word the word to be added. */ public void addWord(String word) { Integer counter = processor.get(word); if(counter == null){ processor.put(word, 1); } else{ processor.put(word, counter + 1); } } /** * Get the number of times the given word has added * via addWord. * @param word The word to be looked up. * @return How many times the word has been added. */ public int howMany(String word) { return processor.getOrDefault(word, 0); } /** * Return the number of distinct words str is a * suffix of in the words added so far. * * It counts as a suffix if a word ends with * str but str does not also match the whole word. * For instance: "thing" matches the following 3 examples: * "something" "nothing" "thing" * but only the first two qualify as proper suffix matches. * * Each word that contains the suffix must only be counted once, * so if "something" has been added 3 times, that only counts * as 1 match. * @param str The suffix to look for. * @return the number of distinct words with the given suffix. */ public int suffixCount(String str) { int counter = 0; for(String word : processor.keySet()){ if(word.endsWith(str) && !word.equals(str)){ counter++; } } return counter; } /** * Count the number of distinct case-insensitive anagrams * of the given word that have been added to the processor. * * An anagram consists of exactly the same number of * letters as the given word, each character occurring * the same number of times. It does not count as an * anagram if the two words are already identical. * * For instance: "and" is an anagram of "Dan" but * "nan" is neither an * @param word The word to be compa anagram of "naan" nor "nan". *red. * @return the number of case-insensitive anagrams * of word. */ public int anagramCount(String word) { int count = 0; int[] wordFreq = new int[26]; word = word.toLowerCase(); for(char ch : word.toCharArray()){ wordFreq[ch-'a']++; } Set checked = new HashSet<>(); for(String str : processor.keySet()){ int[] strFreq = new int[26]; String s = str.toLowerCase(); if(checked.contains(s)){ continue; } checked.add(s); for(char ch : s.toCharArray()){ strFreq[ch-'a']++; } boolean isAnagram = true; for(int i=0; i<26; i++){ if(wordFreq[i] != strFreq[i]){ isAnagram = false; break; } } if(isAnagram){ count++; } } return count; } }

I need help with my anagram code it is failing all the test :

public void anagramNone() { testInstance.addWord("and"); assertEquals(0, testInstance.anagramCount("and")); }

@Test public void anagramNoneCaseSensitive() { testInstance.addWord("and"); testInstance.addWord("AND"); assertEquals(0, testInstance.anagramCount("and")); assertEquals(0, testInstance.anagramCount("AND")); }

@Test public void anagramOne1() { testInstance.addWord("and"); testInstance.addWord("dan"); assertEquals(1, testInstance.anagramCount("dan")); assertEquals(1, testInstance.anagramCount("and")); }

@Test public void anagramOne2() { testInstance.addWord("and"); testInstance.addWord("AND"); testInstance.addWord("dan"); testInstance.addWord("Dan"); testInstance.addWord("DAN"); assertEquals(1, testInstance.anagramCount("dan")); assertEquals(1, testInstance.anagramCount("and")); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!