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())));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大