check number of a given character occurence in a String

后端 未结 6 909
天涯浪人
天涯浪人 2021-01-28 13:23

I want to write a simple class to process Strings (may be very long strings up to 1mil characters in it). String will basically consists of two characters \"a\" and \"b\" that

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-28 13:37

    Why do you need regular expression and split the string for this! You can simply loop through the string and count the number of a and bs. You need to keep two different counter, one for a and one for b. Using regular expression will be less efficient. There is no way you can get the result without traversing the string at least once. So use a simple loop to count a and b.

    • You can make one optimization in the loop. If anytime mod of countA - countB is greater than the number of remaining characters then a and b can never be equal. So you can break the loop then.

    • If the length of the string is odd then there is no need to count. Count of a and b can never be equal when total number of elements is odd.

提交回复
热议问题