0

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");
    }

}
resu
  • 321
  • 1
  • 6
  • 18

3 Answers3

0

have you consider to split your phrase into word and then iterate on every word using your method 'OtherCountLetters'?

instead of returning nothing, just return the highest score of repeated character. Then, in the loop , you just have to compare it with the current maximum

At the end of the loop, you might be able to give the word of the phrase with highest repeated character

Xtof
  • 222
  • 1
  • 5
0

It should be:

public class OtherCountLetters {
    void countLetters2(String str) {
        Map<Character, Integer> numChars = new HashMap<Character, Integer>();
        for (int j = 0; j < str.length(); j++) {
             char charAt = str.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();
        String[] words = "Today is the greatest day ever".toLowerCase().split(" ");
        for (int i = 0; i < words.length; i++) {
            ocl.countLetters2(words[i]);
        }
    }

}
chengpohi
  • 14,064
  • 1
  • 24
  • 42
0

Well the reason you are getting the output that you are is that you are using one hashmap to store all of your words into it. You did a fine job in splitting the words of the sentence into an array, and you iterate over them exactly as you should; however, at the end of the day you are storing the letters in one hash table which confounds which word in the sentence had what.

What I would do is change your solution by making a function that takes in a word and returns an integer that is the number of repetitions of the most frequent letter and then perform this function on each word in the sentence. Then you can compare which word had the highest number.

Hope this helped. I didn't want to give you the code to fix you solution for obvious reasons. (didnt want to rob you of the aha moment.)

Cheers, Andrew malta

Andrew Malta
  • 850
  • 5
  • 15