如何统计字符串中字符出现的频率?
我需要编写某种循环来计算字符串中每个字母的频率。
例如:“aasjjikkk”会计算 2 个“a”、1 个“s”、2 个“j”、1 个“i”、3 个“k”。最终 id 像这些一样最终出现在一个以字符为键、计数为值的映射中。有什么好主意如何做到这一点吗?
I need to write some kind of loop that can count the frequency of each letter in a string.
For example: "aasjjikkk" would count 2 'a', 1 's', 2 'j', 1 'i', 3 'k'. Ultimately id like these to end up in a map with the character as the key and the count as the value. Any good idea how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(27)
您可以使用Multiset(来自番石榴)。它会给你每个对象的计数。例如:
然后对于每个字符,您可以调用
chars.count('a')
并返回出现的次数You can use a Multiset (from guava). It will give you the count for each object. For example:
Then for each character you can call
chars.count('a')
and it returns the number of occurrences这是另一种解决方案,尽管它可能很狡猾。
Here is another solution, dodgy as it may be.
由于没有 Java 8 解决方案,因此考虑发布一个。此外,这个解决方案比这里提到的其他一些解决方案更加整洁、可读和简洁。
Since there was no Java 8 solution, thought of posting one. Also, this solution is much neater, readable and concise than some of the other solutions mentioned here.
嗯,我想到了两种方法,这取决于您的偏好:
按字符对数组进行排序。然后,计算每个字符就变得微不足道了。但您必须首先复制该数组。
创建另一个大小为 26 的整数数组(例如 freq),str 是字符数组。
for(int i = 0; i < str.length; i ++)
freq[str[i] - 'a'] ++; //假设所有字符均为小写
因此 'a' 的数量将存储在 freq[0] 中,'z' 的数量将存储在 freq[25] 中
Well, two ways come to mind and it depends on your preference:
Sort the array by characters. Then, counting each character becomes trivial. But you will have to make a copy of the array first.
Create another integer array of size 26 (say freq) and str is the array of characters.
for(int i = 0; i < str.length; i ++)
freq[str[i] - 'a'] ++; //Assuming all characters are in lower case
So the number of 'a' 's will be stored at freq[0] and the number of 'z' 's will be at freq[25]
这是一个解决方案:
定义您自己的
Pair
:然后您可以这样做:
Here is a solution:
Define your own
Pair
:Then you could do:
在Java8中使用一行
Using one line in Java8
您可以使用 Eclipse Collections 中的
CharAdapter
和CharBag
并避免对Character
和Integer
进行装箱。注意:我是 Eclipse Collections 的提交者。
You can use a
CharAdapter
and aCharBag
from Eclipse Collections and avoid boxing toCharacter
andInteger
.Note: I am a committer for Eclipse Collections.
还有一种选择,它看起来相当不错。
从 java 8 开始,有新的方法 merge java doc
或者使用 ForEach 更干净
There is one more option and it looks quite nice.
Since java 8 there is new method merge java doc
Or even cleaner with ForEach
输入 = aaaajjjgggtttssvvkkllaaiiikk
输出 = a6s2t3v2g3i3j3k4l2
Input = aaaajjjgggtttssvvkkllaaiiikk
Output = a6s2t3v2g3i3j3k4l2
如果这不需要超快,只需创建一个整数数组,每个字母一个整数(只有字母,所以 2*26 整数?或任何可能的二进制数据?)。一次遍历字符串一个字符,获取负责整数的索引(例如,如果只有字母字符,则可以将“A”放在索引 0 处,并通过减去任何“A”到“Z”来获取该索引“A”只是作为如何获得相当快的索引的示例)并增加该索引中的值。
有各种微观优化可以使其更快(如果需要)。
If this does not need to be super-fast just create an array of integers, one integer for each letter (only alphabetic so 2*26 integers? or any binary data possible?). go through the string one char at a time, get the index of the responsible integer (e.g. if you only have alphabetic chars you can have 'A' be at index 0 and get that index by subtracting any 'A' to 'Z' by 'A' just as an example of how you can get reasonably fast indices) and increment the value in that index.
There are various micro-optimizations to make this faster (if necessary).
您可以使用哈希表,其中每个字符作为键,总计数成为值。
You can use a Hashtable with each character as the key and the total count becomes the value.
这与 xunil154 的答案类似,只不过将字符串制成 char 数组,并使用链接的哈希图来维护字符的插入顺序。
This is similar to xunil154's answer, except that a string is made a char array and a linked hashmap is used to maintain the insertion order of the characters.
使用 HashMap 的简短可能代码。 (没有强力的线路保存)
The shorted possible code using a HashMap. (With no forceful line saves)
请尝试下面给出的代码,希望对您有帮助,
示例输出:
输入图案
男人
输入字符串
迪曼曼
FREQUENCY==2
谢谢。编码愉快。
Please try the given code below, hope it will helpful to you,
SAMPLE OUTPUT:
enter the pattern
man
enter the String
dhimanman
FREQUENCY==2
Thank-you.Happy coding.
使用集合频率方法来统计 char* 的频率
Use collections frequency method to count frequency of char*
呃。您不认为这是最简单的解决方案吗?
Uffh. Don't you think this is the simplest solution?
为此,我们可以使用 Collections 类的频率方法。
将字符串拆分为字符串数组。使用HashSet删除重复项并使用Collections的频率方法检查HashSet中每个对象的频率
We can use frequency method of Collections class for this.
Split the string into string array. Use HashSet to remove duplicates and check frequency of each object in HashSet using frequency method of Collections
这是计算字符串中字符频率的更有效方法
This is more Effective way to count frequency of characters in a string
另一种使用地图合并方法的方法
Another way using map merge method
#来自C语言
#From C language
您可以使用 java Map 将
char
映射到int
。然后,您可以迭代字符串中的字符并检查它们是否已添加到映射中,如果有,则可以增加其值。例如:
最后,您将获得遇到的所有字符的计数,并且可以从中提取它们的频率。
或者,您可以使用 Bozho 的解决方案,即使用 Multiset 并计算总出现次数。
You can use a java Map and map a
char
to anint
. You can then iterate over the characters in the string and check if they have been added to the map, if they have, you can then increment its value.For example:
At the end you will have a count of all the characters you encountered and you can extract their frequencies from that.
Alternatively, you can use Bozho's solution of using a Multiset and counting the total occurences.
从 JDK-8 开始使用流 API:
或者如果您希望将值作为
Integer
:另一种变体:
Using the stream API as of JDK-8:
or if you want the values as
Integer
s:Another variant:
执行此操作的一种简洁方法是:
我们使用 for-each 循环遍历每个字符。如果 key 存在,则 getOrDefault 方法获取值或返回(默认)其第二个参数。
或者,可以使用
merge
方法;它可以方便地采用remappingFunction
参数。在本例中,使用Integer::sum
函数。A concise way to do this is:
We use a for-each to loop through every character. The
getOrDefault
method gets the value, if key is present or returns (as default) its second argument.Alternatively, the
merge
method can be used; which conveniently takes aremappingFunction
parameter. In this case theInteger::sum
function is used.