Alternating Characters

  • + 0 comments

    Java:

    public static int alternatingCharacters(String s) {
      if (s.length() <= 1) {
        return 0;
      }
    
      int deletions = 0;
    
      for (int i = 1; i < s.length(); i++) {
        if (s.charAt(i) == s.charAt(i - 1)) {
          deletions++;
        }
      }
      return deletions;
    }