What is the most efficient way to detect if a string contains a number of consecutive duplicate characters in C#?

前端 未结 6 1975
旧时难觅i
旧时难觅i 2021-02-15 22:20

For example, a user entered \"I love this post!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\"

the consecutive duplicate exclamation mark \"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\" should b

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-15 23:11

    The better way i my opinion is create a array, each element in array is responsible for one character pair on string next to each other, eg first aa, bb, cc, dd. This array construct with 0 on each element.

    Solve of this problem is a for on this string and update array values. You can next analyze this array for what you want.

    Example: For string: bbaaaccccdab, your result array would be { 2, 1, 3 }, because 'aa' can find 2 times, 'bb' can find one time (at start of string), 'cc' can find three times.

    Why 'cc' three times? Because 'cc'cc & c'cc'c & cc'cc'.

提交回复
热议问题