Question: why does it always return zero. import java.util.*; public class TextProcessor { private HashMap processor; public TextProcessor() { processor = new HashMap (); } public
why does it always return zero.
import java.util.*; public class TextProcessor { private HashMap processor; public TextProcessor() { processor = new HashMap<>(); } public void addWord(String word) { processor.put(word, processor.getOrDefault(word, 0)+1); } public int howMany(String word) { return processor.getOrDefault(word, 0); } public int suffixCount(String str) { int count = 0; for(String word : processor.keySet()){ if(word.endsWith(str)){ count++; } } return count; } 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; } } Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
