如何使用Java在字符串中找到最常出现的字符?

IT_*_*lic 2 java algorithm histogram

给定一个段落作为输入,找出最常出现的字符。请注意,字符的大小写无关紧要。如果不止一个字符具有相同的最大出现频率,则返回所有字符我正在尝试这个问题,但最终什么也没得到。以下是我尝试过的代码,但它有很多我无法纠正的错误:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

man*_*sta 5

为什么不简单地使用N字母桶(N=字母表中的字母数)?只需沿着字符串并增加相应的字母桶。时间复杂度O(n),空间复杂度O(N)


小智 5

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

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

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;
    }
}
Run Code Online (Sandbox Code Playgroud)