duanchi6397 2016-11-29 13:00
浏览 36
已采纳

计算字符串行中值的出现次数[关闭]

I have a row like this: a a b b b a b a a b b I need code to print this out: 2a 3b 1a 1b 2a 2b

How to do that in c#

EDIT I need the values to be counted like this: there are 2 of a then 3 of b then 1 of a then 1 of b then 2 of a then 2 of b. I don't need to count occurrences of a and b in total.

I am reading a matrix, value by value diagonally: If I have matrix like this:

 a a b a
 a b a b
 a b b a

I managed to read matrix from the bottom right corner - up and to put values in rows:

1row: a
2row: bb
3row: baa
4row: bba
5row: aa
6row: a

But I need this to look like this:

1row: 1a
2row: 2b
3row: 1b2a
4row: 2b1a
5row: 2a
6row: 1a
  • 写回答

1条回答 默认 最新

  • dqlk31541 2016-11-29 13:11
    关注

    Here is a RegEx approach

    string input = "aabbbabaabb";
    string result = string.Concat(Regex.Matches(input, @"(.)\1*", RegexOptions.None)
                          .Cast<Match>()
                          .Select(x => string.Concat(x.Length, x.Value.First())));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?