Java程序找到最多出现的字母?

时间:2017-01-16 18:33:14

标签: java

我有一个句子,我想找到出现在大多数单词中的字符,以及它出现的单词数量。 例如:"我喜欢拜访住在佛罗里达州奥兰多市的朋友威尔。"              哪个应输出I 8。 这是我的代码:

        char maxChar2 = '\0';
        int maxCount2 = 1;
        for (int j=0; j<strs2.length; j++) {
        int charCount = 1;
        char localChar = '\0';
        for (int k=0; k<strs2[j].length(); k++) {
            if (strs2[j].charAt(k) != ' ' && strs2[j].charAt(k) != maxChar2) {
                for (int l=k+1; l<strs2[j].length(); l++) {    
                    if (strs2[j].charAt(k)==strs2[j].charAt(l)) {
                        localChar = strs2[j].charAt(k);
                        charCount++;
                    }
                }
            }
        }
        if (charCount > maxCount2) {
            maxCount2 = charCount;
            maxChar2 = localChar;
        }
    }

,其中strs2是一个String数组。 我的计划正在给我O 79。此外,大写和小写无关紧要,避免使用所有标点符号。

2 个答案:

答案 0 :(得分:0)

作为提示,请尝试使用更有意义的变量名称和正确的缩进。这将有很大帮助,特别是当您的程序没有按照您认为应该执行的操作时。从小开始并为它编写一些测试将有助于一堆。不是完整的句子,而是使用2个单词,然后是3个单词,然后是更复杂的句子。

重写代码以使其更具可读性:

// Where sentence is: "I like".split(" ");
private static void getMostFrequentLetter(String[] sentence) {
    char mostFrequentLetter = '\0';
    int mostFrequentLetterCount = 1;

    for (String word : sentence) {
        int charCount = 1;
        char localChar = '\0';

        for (int wordIndex = 0; wordIndex < word.length(); wordIndex++) {
            char currentLetter = word.charAt(wordIndex);

            if (currentLetter != ' ' && currentLetter != mostFrequentLetter) {
                for (int l = wordIndex + 1; l < word.length(); l++) {
                    char nextLetter = word.charAt(l);

                    if (currentLetter == nextLetter) {
                        localChar = currentLetter;
                        charCount++;
                    }
                }
            }
        }

        if (charCount > mostFrequentLetterCount) {
            mostFrequentLetterCount = charCount;
            mostFrequentLetter = localChar;
        }
    }
}

现在我所做的就是重命名变量并将for循环更改为for-each循环。通过这样做,您可以更清楚地看到您的算法以及您要做的事情。基本上你会查看每个单词并将当前字母与下一个字母进行比较以检查重复项。如果我使用“我喜欢”运行,我应该i 2,但我得到null char 1。您没有正确比较和保存常用字母。这并没有给你答案,但我希望这能让你的代码更清楚,以便你可以修复它。

答案 1 :(得分:-1)

这是一个更优雅的解决方案

public static void FindMostPopularCharacter(String input)
{
    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    input = input.toUpperCase();
    HashMap<Character, Integer> charData = new HashMap<>();
    char occursTheMost = 'A'; //start with default most popular char
    int maxCount = 0;

    //create the map to store counts of all the chars seen
    for(int i = 0; i < alphabet.length(); i++)
        charData.put(alphabet.charAt(i), 0);


    //first find the character to look for
    for(int i = 0; i < input.length(); i++)
    {
        char c = input.charAt(i);
        //if contained in our map increment its count
        if(charData.containsKey(c))
            charData.put(c, charData.get(c) + 1);
        //check for a max count and set the values accordingly
        if(charData.containsKey(c) && charData.get(c) > maxCount)
        {
            occursTheMost = c;
            maxCount = charData.get(c);
        }
    }
    //final step
    //now split it up into words and search which contain our most popular character
    String[] words = input.split(" ");
    int wordCount = 0;
    CharSequence charSequence;
    for(Character character : charData.keySet())
    {
        int tempCount = 0;
        charSequence = "" + character;
        for(int i = 0; i < words.length; i++)
        {
            if(words[i].contains(charSequence))
                tempCount++;
        }

        if(tempCount > wordCount)
        {
            occursTheMost = character;
            wordCount = tempCount;
        }
    }

    System.out.println(occursTheMost + " " + wordCount);
}

的输出
String input = "I like visiting my friend Will, who lives in Orlando, Florida.";
FindMostPopularCharacter(input);

I 8

注意:如果有绑定,则只会输出首次达到最大出现次数的字符。

FindMostPopularCharacter("aabb aabb aabb bbaa");

输出

B 4

因为输入中的最后一个字,B在A之前达到最大值。

FindMostPopularCharacter("aab aab b")

B 3