简体   繁体   English

计算字符串中每个字符出现的次数并将其放入数组中

[英]Count the occurrence of each character in a string and put it in an array

So I'm counting the occurrences of each letter in a string from a Java file I scanned earlier on.因此,我正在计算我之前扫描的 Java 文件中字符串中每个字母的出现次数。 I've removed all unnecessary characters from the string and the string now contains only unicode letters.我从字符串中删除了所有不必要的字符,现在字符串只包含 unicode 字母。 An example:一个例子:

String letters = "wecanendupclimbingthewrongladderandpursuesomeoneelsesversionofsuccess";

So let's say I create an array to contain the frequency of each letter.所以假设我创建了一个数组来包含每个字母的频率。

int[] frequency = new int[26];

And I'm putting the occurrences in order according to the letters in the alphabet.我根据字母表中的字母将事件按顺序排列。 So let's say a is 5, and b is 3 and c is 10. It should be something like this:所以假设a是5,b是3,c是10。应该是这样的:

frequency = {5, 3, 10};

How best do you think I can do this?你认为我能做到什么程度最好?

Calculate the index of character related to the frequency array by (ch - 'a')通过(ch - 'a')计算与frequency数组相关的字符索引

Subtract character 'a' from the character从字符中减去字符'a'

        String letters = "aaaabbabbz";
        letters = letters.toLowerCase();
        int[] frequency = new int[26];
        for (char ch : letters.toCharArray()) {
            int index = ch - 'a';
            frequency[index]++;
        }
        System.out.println(Arrays.toString(frequency));

        // in case you want only the characters that appear you can use IntStream and filter
        int[] result = IntStream.of(frequency).filter(i -> i > 0).toArray();
        System.out.println(Arrays.toString(result));

, output , output

[5, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
[5, 4, 1]

The good solution would be using a TreeMap in order to keep the characters sorted alphabetically and then get the corresponding list of values (number of occurrences) after looping over the characters.好的解决方案是使用TreeMap以保持字符按字母顺序排序,然后在遍历字符后获取相应的值列表(出现次数)。

Here is an example:这是一个例子:

String str = "wecanendupclimbingthewrongladderandpursuesomeoneelsesversionofsuccess";
Map<Character,Integer> map = new TreeMap<>();
for(char c : str.toCharArray())
    map.put(c, map.getOrDefault(c, 0)+1);
System.out.println(map.values());

Output: Output:

[3, 1, 4, 4, 11, 1, 2, 1, 3, 3, 2, 7, 5, 2, 4, 8, 1, 4, 1, 2]

If you want to include absent characters (those with zero occurrences in the string), you can initialize the map with all the alphabets at first as follows:如果要包含不存在的字符(字符串中出现零次的字符),可以首先使用所有字母初始化 map,如下所示:

for(int i = 0; i < 26; i++)
    map.put((char)(97 + i), 0);

Then, the output would be:然后,output 将是:

[3, 1, 4, 4, 11, 1, 2, 1, 3, 0, 0, 3, 2, 7, 5, 2, 0, 4, 8, 1, 4, 1, 2, 0, 0, 0]

Plenty of ways to skin this cat, I think the easiest thing might be sort based off of the character ASCII value.给这只猫剥皮的方法有很多,我认为最简单的方法可能是根据字符 ASCII 值进行排序。 So for example 'a' is valued at 97, so whatever letter you encounter just cast it and subtract 97 to fit it into the proper place in the array (assuming you want arr[0] = frequency of 'a', arr[1] = frequency of 'b', etc).例如,'a' 的值是 97,所以无论你遇到什么字母,只需将其转换并减去 97 以将其放入数组中的适当位置(假设你想要 arr[0] = 'a' 的频率,arr[1 ] = 'b' 的频率等)。 So if you occurred letter 'letter', just increment the array value at index int(letter) - 97, if that makes sense.因此,如果您出现字母“字母”,只需在索引 int(letter) - 97 处增加数组值,如果这有意义的话。 Again, plenty of ways to solve this problem and your thought process is good.同样,有很多方法可以解决这个问题,你的思考过程也很好。

Or as mentioned in the comments you could just use a key/value map lol或者如评论中所述,您可以只使用键/值 map 哈哈

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM