Alternating Characters

  • + 0 comments
    public static int alternatingCharacters(String str) {
        int firstptr=0;
        int secondptr = firstptr+1;
        int totaldeletions = 0;
        while(secondptr < str.length()) {
            if(str.charAt(firstptr) == str.charAt(secondptr)) {
                totaldeletions++;
                secondptr++;
            } else {
                firstptr = secondptr;
                secondptr++;
            }
        }
        return totaldeletions;        
    }