title: check number of a given character occurence in a String
body: <p>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 may be intermingled with each other.  If the number of a's equal number of b's then app will say it is OK otherwise NOK.  I wonder how to do that most efficiently.  I thought of using regex to split String and then count occurence of a's and b's but maybe someone knows better way to do that.  Am relatively new to regex so please let me know should there be any bugs.  This is my early attempt.</p>
  
  <pre><code>public class Typo { 
      public static void main(String[] args){     
          String ver = "";
          int na = 0;
          int nb = 0;
          String regex = ("\\w.+");
          Pattern p = Pattern.compile(regex);
          String text = "ababababbaba";
          if (text.length() == 0){
              ver = "OK";
          }
          else if (text.length() == 1){
              ver = "NOK";
          }
          else if ((text.length() % 2) == 1){
              ver = "NOK";
          }
          else if ((text.length() % 2) == 0){
              //check number of a and b and if it equals return OK otherwise NOK
              Matcher m1 = p.matcher("a");
              while(m1.find()){
                  na = na + 1;
              }
              Matcher m2 = p.matcher("b");
              while(m2.find()){
                  nb = nb + 1;
              }
              if (na == nb){
                  ver = "OK";
              }
              else 
                  ver = "NOK";
          }
          System.out.println(ver);
      }
  
  }
  </code></pre>