Alternating Characters

  • + 0 comments

    Here is a python O(n) time and O(1) space solution:

    def alternatingCharacters(s):
        ch = s[0]
        remove = 0
        
        for i in range(1, len(s)):
            if s[i] == ch:
                remove += 1
            
            if s[i] != ch:
                ch = s[i]
        
        return remove