Intuition

Since shift and capslock does not affect, that’s just a fancy way of saying the string is case in-sensitive. So this boils down the problem to detecting how many times are we changing adjacent characters in s.lower().

For Python devs, we use itertools.pairwise() - this returns pairwise tuples of the entire array. Learn more about it here

Code

Python3

def countKeyChanges(self, s: str) -> int:
 
    ret = 0
 
    for c, n in itertools.pairwise(list(s.lower())):
        ret += (1 if c != n else 0)
    
    return ret

Python3 One Liner

def countKeyChanges(self, s: str) -> int:
    return sum([1 for c, n in itertools.pairwise(list(s.lower())) if c != n])

Big O Analysis

  • Runtime

    The runtime complexity here is since we would be visiting all characters in the string of length N.

  • Memory

    The memory usage is since we are not using any extra data structure.

— A

GitHub | Twitter