Number of Changing Keys

easy string

Problem

You are given a string s typed by a user. Count the number of times the user had to change the key used. Changing a key is using a key different from the last used key; modifiers like shift and caps lock do not count, so case is ignored ('a' and 'A' are the same key).

Inputs = "aAbBcC"
Output2
a→A same key, A→b change, b→B same key, B→c change, c→C same key. Two changes.

def count_key_changes(s):
    changes = 0
    for i in range(1, len(s)):
        if s[i].lower() != s[i - 1].lower():
            changes += 1
    return changes
function countKeyChanges(s) {
  let changes = 0;
  for (let i = 1; i < s.length; i++) {
    if (s[i].toLowerCase() !== s[i - 1].toLowerCase()) {
      changes++;
    }
  }
  return changes;
}
class Solution {
    public int countKeyChanges(String s) {
        int changes = 0;
        for (int i = 1; i < s.length(); i++) {
            if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(i - 1))) {
                changes++;
            }
        }
        return changes;
    }
}
int countKeyChanges(string s) {
    int changes = 0;
    for (int i = 1; i < (int)s.size(); i++) {
        if (tolower(s[i]) != tolower(s[i - 1])) {
            changes++;
        }
    }
    return changes;
}
Time: O(n) Space: O(1)