check number of a given character occurence in a String

后端 未结 6 825
天涯浪人
天涯浪人 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:28

    What's wrong with using something simple as this? Your idea of doing something as simple as this is an overkill, and would end up using more resources.

    String s = "abbb";
    
    int a = 0;
    int b = 0;
    for(int i = 0; i<s.length(); i++){
        if((s.charAt(i) == 'a')){
            a += 1;
        } else {
            b += 1;
        }
    }
    

    a = 1; b = 3

    0 讨论(0)
  • 2021-01-28 13:29

    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
    
    0 讨论(0)
  • 2021-01-28 13:33

    I believe this is what you want:

    private static boolean check(String input) {
        int count = 0;
        for (int i = 0; i < input.length(); ++i) {
            if (input.charAt(i) == 'a') {
                count++;
            }
        }
        return count == input.length() >> 1; // count == input.length()/2
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-28 13:42
    public class Typo { 
        public static void main(String[] args){     
            String ver = "NOK";
    
            String text = "ababababbaba";
    
            if( (text.length() - text.replaceAll("a","").length() ) ==  
                ( text.length() - text.replaceAll("b","").length() ) ) {
                ver = "OK";     
            }
    
            System.out.println(ver);
        }
    }
    
    0 讨论(0)
  • 2021-01-28 13:46

    You should definitely not using regexp for this problem: generally speaking, regexp is not good when you need to count anything. You cannot even write a regexp to check if brackets in an expression are balanced.

    For this problem a simple counter will be sufficient: increment on a, decrement on b, check for zero in the end to know the answer to your problem.

    boolean check(String s) {
        int count = 0;
        for (int i = 0 ; i != s.length() ; i++) {
            if (s.charAt(i) == 'a') {
                count++;
            } else { /* it is b */
                count--;
            }
        }
        return count == 0;
    }
    
    0 讨论(0)
提交回复
热议问题