如何使用Java查找字符串中最频繁出现的字符? [英] How to find the most frequently occurring character in a string with Java?

查看:95
本文介绍了如何使用Java查找字符串中最频繁出现的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个段落作为输入,找到最频繁出现的字符。注意,字符的大小写无关紧要。如果一个以上的字符具有相同的最大出现频率,请将所有字符都返回
我正在尝试此问题,但最终没有结果。以下是我尝试的代码,但是有很多错误我无法纠正:

Given a paragraph as input, find the most frequently occurring character. Note that the case of the character does not matter. If more than one character has the same maximum occurring frequency, return all of them I was trying this question but I ended up with nothing. Following is the code that I tried but it has many errors I am unable to correct:

public class MaximumOccuringChar {

    static String testcase1 = "Hello! Are you all fine? What are u doing today? Hey Guyz,Listen! I have a plan for today.";

    public static void main(String[] args) 
    {
        MaximumOccuringChar test = new MaximumOccuringChar();
        char[] result = test.maximumOccuringChar(testcase1);
        System.out.println(result);
    }

    public char[] maximumOccuringChar(String str) 
    {
        int temp = 0;
        int count = 0;
        int current = 0;

        char[] maxchar = new char[str.length()];

        for (int i = 0; i < str.length(); i++) 
        {
            char ch = str.charAt(i);

            for (int j = i + 1; j < str.length(); j++) 
            {
                char ch1 = str.charAt(j);

                if (ch != ch1) 
                {
                    count++;
                }
            }

            if (count > temp) 
            {
                temp = count;
                maxchar[current] = ch;
                current++;
            }
        }
        return maxchar;
    }
}


推荐答案

您已经在这里得到您的答案: https://stackoverflow.com/a/21749133/1661864

You already got your answer here: https://stackoverflow.com/a/21749133/1661864

这是我能想到的最简单的方法。

It's a most easy way I can imagine.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MaximumOccurringChar {

    static final String TEST_CASE_1 = "Hello! Are you all fine? What are u doing today? Hey Guyz,Listen! I have a plan for today. Help!";


    public static void main(String[] args) {
        MaximumOccurringChar test = new MaximumOccurringChar();
        List<Character> result = test.maximumOccurringChars(TEST_CASE_1, true);
        System.out.println(result);
    }


    public List<Character> maximumOccurringChars(String str) {
        return maximumOccurringChars(str, false);
    }

    // set skipSpaces true if you want to skip spaces
    public List<Character> maximumOccurringChars(String str, Boolean skipSpaces) {
        Map<Character, Integer> map = new HashMap<>();
        List<Character> occurrences = new ArrayList<>();
        int maxOccurring = 0;

        // creates map of all characters
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);

            if (skipSpaces && ch == ' ')      // skips spaces if needed
                continue;

            if (map.containsKey(ch)) {
                map.put(ch, map.get(ch) + 1);
            } else {
                map.put(ch, 1);
            }

            if (map.get(ch) > maxOccurring) {
                maxOccurring = map.get(ch);         // saves max occurring
            }
        }

        // finds all characters with maxOccurring and adds it to occurrences List
        for (Map.Entry<Character, Integer> entry : map.entrySet()) {
            if (entry.getValue() == maxOccurring) {
                occurrences.add(entry.getKey());
            }
        }

        return occurrences;
    }
}

这篇关于如何使用Java查找字符串中最频繁出现的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆