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
If you want you can use a third party library like StringUtils. It has a method countMatches which will do the work.
StringUtils.countMatches("abba", "a") = 2 StringUtils.countMatches("abba", "ab") = 1