Alternating Characters

  • + 0 comments

    This is my Java 8 (15) solution using REGEX, feel free to ask me any questions.

    public static int alternatingCharacters(String s) {
        Matcher m = Pattern.compile("([AB])\\1+").matcher(s);
        return s.length() - m.replaceAll("$1").length();
    }