I am trying to solve the programming challenge outlined below, wherein basically you need to find the word in a given sentence with the greatest number of repeated characters. I have struggled a bit with this, and was lucky enough to find some code for counting the occurrences of letters in a string (also below). This particular piece of code store all the letters in a HashMap, and I need to tailor it so that it stores the character occurrence of each word separately (instead of in aggregate, as its doing at the moment). This is where I am stuck. What could I use to store the state of a HashMap with each iteration of its loop?
/* Using the Java language, have the function LetterCountI(str) take
* the str parameter being passed and return the first word with the
* greatest number of repeated letters. For example: "Today, is the
* greatest day ever!" should return greatest because it has 2 e's
* (and 2 t's) and it comes before ever which also has 2 e's. If there
* are no words with repeating letters return -1. Words will be
* separated by spaces. */
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class OtherCountLetters {
void countLetters2(String str) {
String[] words = str.toLowerCase().split(" ");
Map<Character, Integer> numChars = new HashMap<Character, Integer>();
for (int i = 0; i < words.length; i++) {
for (int j = 0; j < words[i].length(); j++) {
char charAt = words[i].charAt(j);
if (!numChars.containsKey(charAt)) {
numChars.put(charAt, 1);
} else {
numChars.put(charAt, numChars.get(charAt) + 1);
}
}
}
System.out.println(numChars);
}
public static void main(String[] args) {
OtherCountLetters ocl = new OtherCountLetters();
ocl.countLetters2("Today is the greatest day ever");
}
}
At the moment, for the sentence "today is the greatest day ever", the program returns
{v=1, g=1, d=2, e=5, t=4, s=2, r=2, a=3, o=1, h=1, y=2, i=1}
But I need it to return something like
{a=1, d=1, o=1, t=1, y=1} //'today'
{i=1, s=1} //'is'
{e=1, h=1, t=1} //'the'
{g=1, t=2, e=2, s=1, r=1, a=1} //'greatest'
{d=1, a=1, y=1} //'day'
{v=1, e=2, r=1} //'ever'
that way, I could iterate over each entry to see which one has the largest value, and then return the corresponding word to the user.
Thanks,
-----EDIT----
After posting this I had a Eureka moment:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class OtherCountLetters {
void countLetters2(String str) {
String[] words = str.toLowerCase().split(" ");
String target = null;
int largest = 0;
Map<Character, Integer> numChars = new HashMap<Character, Integer>();
for (int i = 0; i < words.length; i++) {
for (int j = 0; j < words[i].length(); j++) {
char charAt = words[i].charAt(j);
if (!numChars.containsKey(charAt)) {
numChars.put(charAt, 1);
} else {
numChars.put(charAt, numChars.get(charAt) + 1);
}
if (numChars.get(charAt) > largest) {
largest = numChars.get(charAt);
target = words[i];
}
}
numChars.clear();
}
if (largest != 1) {
System.out.println(target);
} else {
System.out.println("there are no words with 2 or more letters");
}
}
public static void main(String[] args) {
OtherCountLetters ocl = new OtherCountLetters();
ocl.countLetters2("today is the greatest day ever and car");
}
}