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.
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);
}
}
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 - countBis 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.