统计字符串中一行中字符的出现次数。

huangapple go评论98阅读模式
英文:

Count occurences in a row of a char in string

问题

有没有办法在Java中连续计算字符串中字符的出现次数?
例如,给定字符串:ABBB99999SHJ99JI,将输出字符串中9字符连续出现的情况:连续5次出现一次,连续2次出现两次。
我尝试找到解决方案,但没有结果。

可以使用正则表达式找到,但对于每个数字数量,我需要创建一个单独的正则表达式。

英文:

Is there any way to count occurences of a char in string in a row in Java?
For example, given string: ABBB99999SHJ99JI, will output that 9 char was find in a string once: 5 times in a row and twice: 2 times in a row.
I tried to find the solution, but without result.

It is possible to find it with regex, but for each amount of numbers i need to create a separate one.

答案1

得分: 1

可以按照你提到的正则表达式的方法来实现:

  • 创建一个正则表达式来查找至少包含一个字符的分组,例如 ([9]+)
  • 在循环中查找这些分组并收集它们的长度。
  1. public static List<Integer> countGroupLengths(String src, char c) {
  2. Pattern p = Pattern.compile(c + "+"); // 例如 `9+` 也可以
  3. Matcher m = p.matcher(src);
  4. List<Integer> result = new ArrayList<>();
  5. while (m.find()) {
  6. String group = m.group(1);
  7. result.add(group.length());
  8. }
  9. return result;
  10. }

测试:

  1. System.out.println(countGroupLengths("ABBB99999SHJ99JI", '9'));

输出:

  1. [5, 2]

更新
根据评论中 @Andreas 提供的所有更改,可能值得为每个字符构建一个映射到长度列表的映射:

  1. public static Map<Character, List<Integer>> buildMapOfGroups(String src) {
  2. return src.chars() // IntStream
  3. .mapToObj(c -> (char) c)
  4. .map(c -> Map.entry(
  5. c, // 键
  6. Pattern.compile(c + "+").matcher(src)
  7. .results()
  8. .map(r -> r.group().length())
  9. .collect(Collectors.toList())
  10. ))
  11. .collect(Collectors.toMap(
  12. Map.Entry::getKey, Map.Entry::getValue,
  13. (e1, e2) -> e1, LinkedHashMap::new));
  14. }

对于给定的源字符串:System.out.println(buildMapOfGroups("ABBB99999SHJ99JI"));
输出为:

  1. {A=[1], B=[3], 9=[5, 2], S=[1], H=[1], J=[1, 1], I=[1]}

注意:使用 LinkedHashMap 来保持插入顺序

英文:

It can be done using regexp as you mentioned:

  • create a regexp to find groups containing at least one character, e.g. ([9]+)
  • find the groups in a loop and collect their lengths.
  1. public static List&lt;Integer&gt; countGroupLengths(String src, char c) {
  2. Pattern p = Pattern.compile(c + &quot;+&quot;); // e.g. `9+` works too
  3. Matcher m = p.matcher(src);
  4. List&lt;Integer&gt; result = new ArrayList&lt;&gt;();
  5. while (m.find()) {
  6. String group = m.group(1);
  7. result.add(group.length());
  8. }
  9. return result;
  10. }

test:

  1. System.out.println(countGroupLengths(&quot;ABBB99999SHJ99JI&quot;, &#39;9&#39;));

output:

  1. [5, 2]

Update<br/>
It may be worth to build a map for each character to the list of lengths
integrating all the changes offered by @Andreas in the comments:

  1. public static Map&lt;Character, List&lt;Integer&gt;&gt; buildMapOfGroups(String src) {
  2. return src.chars() // IntStream
  3. .mapToObj(c -&gt; (char) c)
  4. .map(c -&gt; Map.entry(
  5. c, // key
  6. Pattern.compile(c + &quot;+&quot;).matcher(src)
  7. .results()
  8. .map(r -&gt; r.group().length())
  9. .collect(Collectors.toList())
  10. ))
  11. .collect(Collectors.toMap(
  12. Map.Entry::getKey, Map.Entry::getValue,
  13. (e1, e2) -&gt; e1, LinkedHashMap::new));
  14. }

For the given source: System.out.println(buildMapOfGroups(&quot;ABBB99999SHJ99JI&quot;));
prints:

  1. {A=[1], B=[3], 9=[5, 2], S=[1], H=[1], J=[1, 1], I=[1]}

Note: using LinkedHashMap to keep the order of insertion

huangapple
  • 本文由 发表于 2020年10月13日 22:50:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64337573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定